java - Difference between running and starting a thread -


this question has answer here:

i don't understand difference between starting , running thread, tested both methods , outputs same result, first used combination of run() , start on same thread , did same function follows:

public class testrunandstart implements runnable { public void run() {     system.out.println("running"); } public static void main(string[] args) {      thread t = new thread(new testrunandstart());      t.run();      t.run();      t.start();  } 

}

the output is:

running running running 

then saw javadoc of run() method said that: if thread constructed using separate runnable run object, runnable object's run method called; otherwise, method nothing , returns tried use run() method without separate runnable follows :

public class testrun extends thread { public void run(){     system.out.println("running normal thread"); } public static void main(string[]args){     testrun tr=new testrun();     tr.run();    }  } 

and executes run() method , prints running normal thread although it's constructed without separate runnable! what's main difference between 2 methods

thread.run() not spawn new thread whereas thread.start() does, i.e thread.run runs on same thread of caller whereas thread.start() creates new thread on task run.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -