start() method:
- This method is responsible for causing the thread (it's called upon) to begin execution, this method only causes and not actually starts the execution. It just schedules the thread and when the Thread Scheduler picks this thread for execution then the JVM calls the run() method to actually start the execution of the thread.
- This method will obviously result into two concurrent threads - one, from which this method is called(main thread) and second, the new thread which executes the run() method of the new Thread instance.
- A thread will throw IllegalStateException in case you try to call the start()method on an already started thread instance. That means, a thread can not be started more than once. As per the Java specifications a thread may not be restarted after completing its execution. You'll be required to create and start the execution of a new thread in that case.
run() method:
This is the only method of the Runnable interface and the classes which intend to execute their code in a separate thread of execution first implement the Runnable interface and subsequently define this method and put all the code expected to be executed in the separate thread inside the scope of this run method.
The other way, such classes can follow for the same is by extending the Thread class and in that case also they should oevrride the run method of the Thread class and put all the code supposed to be executed in the new thread inside the scope of the overriden run method.
Difference between start() and run() methods
start() methods only schedules the thread for execution and not actually begins the execution of the thread. The execution of the thread is started when the JVM calls the run() method of the thread once the CPU Scheduler picks this scheduled thread for execution.
Let’s use start() method to start a thread:
class MyRunnable implements Runnable{
public void run(){ //overrides Runnable's run() method
System.out.println("in run() method");
System.out.println("currentThreadName= "+ Thread.currentThread().getName());
}
}
public class MyClass {
public static void main(String args[]){
System.out.println("currentThreadName= "+ Thread.currentThread().getName());
MyRunnable runnable=new MyRunnable();
Thread thread=new Thread(runnable);
thread.start();
}
}
/*OUTPUT
currentThreadName= main
in run() method
currentThreadName= Thread-0
*/
|
If we note output, when we called start() from main thread, run() method was called by new Thread (i.e. Thread-0).
Let’s use run() method to start a thread:
class MyRunnable implements Runnable{
public void run(){ //overrides Runnable's run() method
System.out.println("in run() method");
System.out.println("currentThreadName= "+ Thread.currentThread().getName());
}
}
public class MyClass {
public static void main(String args[]){
System.out.println("currentThreadName= "+ Thread.currentThread().getName());
MyRunnable runnable=new MyRunnable();
Thread thread=new Thread(runnable);
thread.run();
}
}
/*OUTPUT
currentThreadName= main
in run() method
currentThreadName= main
*/
|
If we note output, when we called run() from main thread, run() method was called by main Thread, not by newly created thread (i.e. Thread-0).
No comments:
Post a Comment