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();
}
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()
andr.run()
than there is between any other two methods.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 forr.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.So, what does
t.start()
do?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.And what does
r.run()
do?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.t.start()
is the method that the library provides for your code to call when you want a new thread.r.run()
is the method that you provide for the library to call in the new thread.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.
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.
invoke
run()
is executing on the calling thread, like any other method call. whereasThread.start()
creates a new thread. invokingrun()
is a programmatic bug.First example: No multiple threads. Both execute in single (existing) thread. No thread creation.
r1
andr2
are just two different objects of classes that implement theRunnable
interface and thus implement therun()
method. When you callr1.run()
you are executing it in the current thread.Second example: Two separate threads.
t1
andt2
are objects of the classThread
. When you callt1.start()
, it starts a new thread and calls therun()
method ofr1
internally to execute it within that new thread.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'srun
method is executed in parallel.