What's the difference between Thread start() and Runnable run()


What's the difference between Thread start() and Runnable run()



Say we have these two Runnables:


class R1 implements Runnable {
public void run() { … }

}

class R2 implements Runnable {
public void run() { … }

}



Then what's the difference between this:


public static void main() {
R1 r1 = new R1();
R2 r2 = new R2();

r1.run();
r2.run();
}



And this:


public static void main() {
R1 r1 = new R1();
R2 r2 = new R2();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);

t1.start();
t2.start();
}




15 Answers
15



First example: No multiple threads. Both execute in single (existing) thread. No thread creation.


R1 r1 = new R1();
R2 r2 = new R2();



r1 and r2 are just two different objects of classes that implement the Runnable interface and thus implement the run() method. When you call r1.run() you are executing it in the current thread.


r1


r2


Runnable


run()


r1.run()



Second example: Two separate threads.


Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);



t1 and t2 are objects of the class Thread. When you call t1.start(), it starts a new thread and calls the run() method of r1 internally to execute it within that new thread.


t1


t2


Thread


t1.start()


run()


r1





Couuld I consider that before we call Thread#start() ,nothing really relative to os thread happens? It is only a java object.
– Jaskey
Dec 6 '14 at 9:38





That's correct according to the documentation. Check the thread object initialization code, which conforms to the documentation. Also in the source code, it's the start(), which is calling a natvie method, which must be making the os thread related things happen.
– Bhesh Gurung
May 28 '15 at 14:44


start()





Thread constructor docs is here. Thread object initialization source is here. start() method source is here.
– Bhesh Gurung
May 28 '15 at 14:47



start()





@borjab, can you elaborate on this?
– Shawn S.
Jun 4 at 20:16



If you just invoke run() directly, it's executed on the calling thread, just like any other method call. Thread.start() is required to actually create a new thread so that the runnable's run method is executed in parallel.


run()


Thread.start()


run





In Hotspot JVM, there is a direct mapping between java thread and native thread. Thread.start() invocation make thread state move from new state to Runnable state. Runnable does not mean thread is running. Once the native thread has initialized, native thread invokes the run() method in the Java thread, which makes thread state change from Runnable to Running. When the thread terminates all resources for both the native and Java thread are released.
– overexchange
Sep 27 '17 at 18:09



Thread.start()


run()





@overexchange Where can I find the material about state changing.
– twlkyao
Feb 1 at 7:41



The difference is that Thread.start() starts a thread that calls the run() method, while Runnable.run() just calls the run() method on the current thread.


Thread.start()


run()


Runnable.run()


run()





Why would an 80k user give an answer like this two years after way better answers have been given and accepted?
– Sebastian
Oct 28 '13 at 9:39





@Sebastian I don't accept your hypothesis. Specifically, the accepted answer is verbose and poor quality, and I don't see any other answers expressed better than this one.
– EJP
Nov 1 '13 at 23:28



The difference is that when program calls start() method, a new thread is created and code inside run() is executed in new thread while if you call run() method directly no new thread will be created and code inside run() will execute in the current thread directly.


start()


run()


run()


run()



Another difference between start() and run() in Java thread is that you can not call start() twice. Once started, second start() call will throw IllegalStateException in Java while you can call run() method several times since it's just an ordinary method.


start()


run()


start()


start()


IllegalStateException


run()



Actually Thread.start() creates a new thread and have its own execution scenario.


Thread.start()



Thread.start() calls the run() method asynchronously,which changes the state of new Thread to Runnable.


Thread.start()


run()



But Thread.run() does not create any new thread. Instead it execute the run method in the current running thread synchronously.


Thread.run()



If you are using Thread.run() then you are not using the features of multi threading at all.


Thread.run()



invoke run() is executing on the calling thread, like any other method call. whereas Thread.start() creates a new thread.
invoking run() is a programmatic bug.


run()


Thread.start()


run()



If you do run() in main method, the thread of main method will invoke the run method instead of the thread you require to run.


run()


run



The start() method creates new thread and for which the run() method has to be done


start()


run()





The 'main method' has nothing to do with it.
– EJP
Oct 28 '13 at 9:25






@EJP, by main the writer meant the calling method. His answer is quite good. +1 ;-)
– dom_beau
Mar 7 '14 at 13:46


main





@dom_beau If that's what he meant he should have said so. What he did say was incorrect. There is nothing 'quite good' about this answer. It's just a confused mess.
– EJP
May 26 '15 at 12:27



Thread.start() code registers the Thread with scheduler and the scheduler calls the run() method. Also, Thread is class while Runnable is an interface.


Thread.start()


run()


Thread


Runnable



The points, that the members made are all right so I just want to add something. The thing is that JAVA supports no Multi-inheritance. But What is if you want to derive a class B from another class A, but you can only derive from one Class. The problem now is how to "derive" from both classes: A and Thread. Therefore you can use the Runnable Interface.


public class ThreadTest{
public void method(){
Thread myThread = new Thread(new B());
myThread.start;
}
}

public class B extends A implements Runnable{...





nicely explanation of run() method with help of an example about Runnable - an interface and Thread - a class
– Pinky Walve
Feb 18 at 11:30



Most of these answers miss the big picture, which is that, as far as the Java language is concerned, there is no more difference between t.start() and r.run() than there is between any other two methods.


t.start()


r.run()



They're both just methods. They both run in the thread that called them. They both do whatever they were coded to do, and then they both return, still in the same thread, to their callers.



The biggest difference is that most of the code for t.start() is native code while, in most cases, the code for r.run() is going to be pure Java. But that's not much of a difference. Code is code. Native code is harder to find, and harder to understand when you find it, but it's still just code that tells the computer what to do.


t.start()


r.run()



So, what does t.start() do?


t.start()



It creates a new native thread, it arranges for that thread to call t.run(), and then it tells the OS to let the new thread run. Then it returns.


t.run()



And what does r.run() do?


r.run()



The funny thing is, the person asking this question is the person who wrote it. r.run() does whatever you (i.e., the developer who wrote it) designed it to do.


r.run()



t.start() is the method that the library provides for your code to call when you want a new thread.


t.start()



r.run() is the method that you provide for the library to call in the new thread.


r.run()





If 'most of the code for t.run() is native code', how does it do 'exactly whatever you (i.e. the developer who wrote it) designed it to do'? Do you mean that most of the code for Thread.start() is native code?
– EJP
Nov 24 '15 at 9:37


t.run()


Thread.start()





@EJP, Oops! Thank you for catching that.
– james large
Nov 24 '15 at 14:40



If you directly call run() method, you are not using multi-threading feature since run() method is executed as part of caller thread.


run()


run()



If you call start() method on Thread, the Java Virtual Machine will call run() method and two threads will run concurrently - Current Thread (main() in your example) and Other Thread (Runnable r1 in your example).


start()


main()


r1



Have a look at source code of start() method in Thread class


start()


/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}

private native void start0();



In above code, you can't see invocation to run() method.


run()



private native void start0() is responsible for calling run() method. JVM executes this native method.


private native void start0()


run()



Suppose you are a Manager of a hotel named Thread. So basically what t1.run() does is that it makes you have to do all the tasks yourself, while your other Thread helpers sit idle twiddling their thumbs. But if you use t1.start(), one of your helpers is assigned some task and then he/she starts doing that task, while you do the job you are supposed to do that is manage the helpers.



Source: Programming Interview: Threads in Operating System (Java ) Part 2 Multithreading





OMG. YouTube is not a normative reference. 'A hotel named Thread' and 'other Thread helpers' just adds to the confusion. Are threads hotels, or helpers? This is just nonsense.
– EJP
Nov 24 '15 at 9:42



Start() method call run override method of Thread extended class and Runnable implements interface.



But by calling run() it search for run method but if class implementing Runnable interface then it call run() override method of Runnable.



ex.:



`


public class Main1
{
A a=new A();
B b=new B();
a.run();//This call run() of Thread because run() of Thread only call when class
//implements with Runnable not when class extends Thread.
b.run();//This not run anything because no run method found in class B but it
//didn't show any error.

a.start();//this call run() of Thread
b.start();//this call run() of Thread
}

class A implements Runnable{
@Override
public void run() {
System.out.println("A ");
}
}

class B extends Thread {

@Override
public void run() {
System.out.println("B ");
}
}



`



In the first case you are just invoking the run() function of the r1 adn r2 objects.



In the second case you're actually creating 2 new Threads! ;) start() will call run() at some point!





Actually, start() won't call run(): if it did, then the run() method would get executed by the same thread that called start(). What start() will do is create a thread which will call the run() method.
– Bruno Reis
Dec 22 '11 at 4:44



The separate start() and run() methods in the Thread class provide two ways to create threaded programs. The start() method starts the execution of the new thread and calls the run() method. The start() method returns immediately and the new thread normally continues until the run() method returns.



The Thread class' run() method does nothing, so sub-classes should override the method with code to execute in the second thread. If a Thread is instantiated with a Runnable argument, the thread's run() method executes the run() method of the Runnable object in the new thread instead.



Depending on the nature of your threaded program, calling the Thread run() method directly can give the same output as calling via the start() method, but in the latter case the code is actually executed in a new thread.





Calling `run()' is not a way to create threaded programs. There is only one way.
– EJP
Aug 23 '16 at 9:02






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails