I was looking a small example on Threads.For creating Threads we can do in 2 ways either by implementing Runnable interface or by extending Thread.I used the 1st way
package test;
public class test implements Runnable{
public static void main(String args[])
{
test t=new test();
t.run();Thread th=Thread.currentThread();
th.start();
}
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("hi");
}
}
My doubt is when we are calling th.start(); then run() is called.I want to know how.I thought internally there start() may be calling run() so I looked in the documentation of Thread class
The following is the start() declaration in Thread class
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();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
As you can see inside start(),run() is not called but when we are calling th.start() then automatically overriden run() is called.Can anybody please throw some light in this
The mechanism whereby the run method is invoked on a new thread is extralinguistic: it cannot be represented in terms of Java code. This is the crucial line in the start method:
start0();
start0 is a native method whose invocation will:
cause a new native thread-of-execution to be created;
cause the run method to be invoked on that thread.
Thread th=Thread.currentThread();
th.start();// its call run method automatically
if you call direct run method with class object than its treat as a normal method not thread method
start() method of Thread class is used to start a newly created thread. It performs following tasks:
A new thread starts(with new callstack).
The thread moves from New state to the Runnable state.
When the thread gets a chance to execute, its target run() method will run.
It is not supposed to call the run method. If it does, it will do it in the same running thread.
Just like a program starts in the main method of a class, a new thread starts in the run method of the class. You have to look into native code to find it.
You can find your answer in documentation of Thread.start() method.
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()
Internally start method calls the run method.
Simple experiment may reveal the fact. Call Thread.start() and Thread.run(),, keep a print message statement inside run method. This message will be shown two times because the run method is called two times.
Check this statements in your question code. started flag was false before calling start0, after calling start0() function starrted flag is made true. it means the run method is called withing start0().
boolean started = false;
try {
start0();
started = true;
}
To be precise, the start0 method creates new thred to execute the run method and it returns the current thread.
From oracle official document
" public void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
Throws:
IllegalThreadStateException - if the thread was already started.
See Also:
run(), stop() "
Related
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();
}
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.
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.
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.
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.
The difference is that when program calls start() method, a new thread is created and code inside run() is executed in the 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.
Another difference between start() and run() in Java thread is that you cannot 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.
Actually Thread.start() creates a new thread and have its own execution scenario.
Thread.start() calls the run() method asynchronously,which changes the state of new Thread to Runnable.
But Thread.run() does not create any new thread. Instead it execute the run method in the current running thread synchronously.
If you are using Thread.run() then you are not using the features of multi threading at all.
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.
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.
The start() method creates new thread and for which the run() method has to be done
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.
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.
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.
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.
Thread.start() code registers the Thread with scheduler and the scheduler calls the run() method. Also, Thread is class while Runnable is an interface.
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{...
If you directly call run() method, you are not using multi-threading feature since run() method is executed as part of caller thread.
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).
Have a look at source code of start() method in Thread class
/**
* 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.
private native void start0() is responsible for calling run() method. JVM executes this native method.
In the first case you are just invoking the run() method of the r1 and r2 objects.
In the second case you're actually creating 2 new Threads!
start() will call run() at some point!
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.
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 ");
}
}
`
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();
}
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.
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.
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.
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.
The difference is that when program calls start() method, a new thread is created and code inside run() is executed in the 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.
Another difference between start() and run() in Java thread is that you cannot 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.
Actually Thread.start() creates a new thread and have its own execution scenario.
Thread.start() calls the run() method asynchronously,which changes the state of new Thread to Runnable.
But Thread.run() does not create any new thread. Instead it execute the run method in the current running thread synchronously.
If you are using Thread.run() then you are not using the features of multi threading at all.
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.
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.
The start() method creates new thread and for which the run() method has to be done
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.
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.
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.
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.
Thread.start() code registers the Thread with scheduler and the scheduler calls the run() method. Also, Thread is class while Runnable is an interface.
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{...
If you directly call run() method, you are not using multi-threading feature since run() method is executed as part of caller thread.
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).
Have a look at source code of start() method in Thread class
/**
* 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.
private native void start0() is responsible for calling run() method. JVM executes this native method.
In the first case you are just invoking the run() method of the r1 and r2 objects.
In the second case you're actually creating 2 new Threads!
start() will call run() at some point!
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.
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 ");
}
}
`
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();
}
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.
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.
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.
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.
The difference is that when program calls start() method, a new thread is created and code inside run() is executed in the 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.
Another difference between start() and run() in Java thread is that you cannot 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.
Actually Thread.start() creates a new thread and have its own execution scenario.
Thread.start() calls the run() method asynchronously,which changes the state of new Thread to Runnable.
But Thread.run() does not create any new thread. Instead it execute the run method in the current running thread synchronously.
If you are using Thread.run() then you are not using the features of multi threading at all.
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.
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.
The start() method creates new thread and for which the run() method has to be done
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.
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.
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.
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.
Thread.start() code registers the Thread with scheduler and the scheduler calls the run() method. Also, Thread is class while Runnable is an interface.
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{...
If you directly call run() method, you are not using multi-threading feature since run() method is executed as part of caller thread.
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).
Have a look at source code of start() method in Thread class
/**
* 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.
private native void start0() is responsible for calling run() method. JVM executes this native method.
In the first case you are just invoking the run() method of the r1 and r2 objects.
In the second case you're actually creating 2 new Threads!
start() will call run() at some point!
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.
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 ");
}
}
`
Code:
public class ThreadTest {
public static void main(String[] args) {
MyImlementThread mit = new MyImlementThread();
Thread t = new Thread(mit);
t.start();
t = new Thread(mit);
t.start();
}
}
// MyImlementThread
class MyImlementThread implements Runnable {
public void run() {
System.out.println("This is implemented run() method");
}
}
/*
Output
This is implemented run() method
This is implemented run() method
*/
What happens here is the main thread starts two threads and exits. Each of the new threads writes a message to stdout, then ends. At that point since all the non-daemon threads have finished the JVM exits.
The posted code is confusing on account of it defining a Runnable but giving it a name ending in Thread.
A Thread object relates to an os-level thread, calling start on a Thread makes the code in the run method of the passed-in Runnable execute run on a separate thread from the one that called start.
The Runnable defines a task but doesn't specify how it runs. It could be passed into a specific Thread's constructor or submitted to an Executor or run by the current thread.
In this case the Runnable declared has no state, no instance variables are declared. Here two threads can execute the same Runnable without a conflict because there is no shared state. The printstream that writes to the console is synchronized, so the lines written by the threads each get written one at a time and don't get jumbled together.
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();
}
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.
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.
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.
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.
The difference is that when program calls start() method, a new thread is created and code inside run() is executed in the 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.
Another difference between start() and run() in Java thread is that you cannot 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.
Actually Thread.start() creates a new thread and have its own execution scenario.
Thread.start() calls the run() method asynchronously,which changes the state of new Thread to Runnable.
But Thread.run() does not create any new thread. Instead it execute the run method in the current running thread synchronously.
If you are using Thread.run() then you are not using the features of multi threading at all.
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.
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.
The start() method creates new thread and for which the run() method has to be done
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.
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.
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.
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.
Thread.start() code registers the Thread with scheduler and the scheduler calls the run() method. Also, Thread is class while Runnable is an interface.
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{...
If you directly call run() method, you are not using multi-threading feature since run() method is executed as part of caller thread.
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).
Have a look at source code of start() method in Thread class
/**
* 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.
private native void start0() is responsible for calling run() method. JVM executes this native method.
In the first case you are just invoking the run() method of the r1 and r2 objects.
In the second case you're actually creating 2 new Threads!
start() will call run() at some point!
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.
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 ");
}
}
`