7

i don't understand the difference between starting and running a thread, i tested the both methods and they outputs the same result, first i used a combination of run() and start on the same thread and they did the same function as 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 i saw the javadoc of the run() method said that: If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns so i tried to use the run() method without a separate runnable as 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 it also executes the run() method and prints Running Normal Thread although it's constructed without a separate runnable! so what's the main difference between the two methods

Java Player
  • 6,050
  • 26
  • 99
  • 154
  • it's not a duplicate! – naXa Mar 24 '15 at 11:50
  • However it is too late. But may be helpful for others.Replace the run method SOP statement by "System.out.println("running thread..." + Thread.currentThread().getName());" and then execute your program. You will see the difference. Output is "running thread...main running thread...main running thread...Thread-0" – user320676 Aug 12 '16 at 06:47

4 Answers4

23

Thread.run() does not spawn a new thread whereas Thread.start() does, i.e Thread.run actually runs on the same thread as that of the caller whereas Thread.start() creates a new thread on which the task is run.

Extreme Coders
  • 3,195
  • 2
  • 33
  • 49
  • ok, but i don't understand the meaning of "If this thread was constructed using a separate Runnable run object " is TestRun class in my example is considered to be constructed without a separate Runnable ?? – Java Player Apr 05 '13 at 19:01
  • @JavaPlayer Check ***[this](http://stackoverflow.com/questions/262816/when-would-you-call-javas-thread-run-instead-of-thread-start)*** post for your answer – Extreme Coders Apr 05 '13 at 19:05
12

When you call run, you're just executing the run method in the current thread. You code thus won't be multi-threaded.

If you call start, this will start a new thread and the run method will be executed on this new thread.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
1

A thread goes through various stages during its life cycle. When you call start() method thread goes in the runnable state and not the running state.

Then the scheduler pics up one thread from pool of thread waiting to get executed and puts it in the running stage. At this actual execution of the thread takes place.

Ankur Shanbhag
  • 7,376
  • 2
  • 26
  • 37
0

In your first example, you are calling the run() method directly from the main thread, and then once from a separate thread by calling t.start().

In the second case, t.start() creates a separate thread context, and from that context, calls t.run().

You also included the docs for the run() method:

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns

This is true, in that if you don't override the run() method, then it will simply return, causing that thread to terminate. You have overridden the run() method in your Thread objects, so the overridden method (which does nothing) is not called.

Jamie
  • 1,786
  • 1
  • 16
  • 20