Difference Betwixt Outset In Addition To Piece Of Job Method Inward Thread – Coffee Tutorial In Addition To Interview Question
Why exercise i telephone holler upward start method of thread if start() calls run() inward turn" or "What is divergence yesteryear calling start() over run() method inward coffee thread" are 2 widely popular beginner marking multi-threading interview question. When a Java programmer start learning Thread, rootage affair he learns is to implement thread either overriding run() method of Thread shape or implementing Runnable interface too than calling start() method on thread, but amongst unopen to sense he finds that start() method calls run() method internally either yesteryear looking API documentation or exactly poking around, but many of us exactly don’t attention at that fourth dimension until its been asked inward Java Interview. In this Java tutorial nosotros volition run into What is divergence betwixt calling start() method too run() method for starting Thread inward Java.
This article is inward continuation of my before post service on Java multi-threading e.g. Difference betwixt Runnable too Thread inward Java too How to solve Producer Consumer work inward Java using BlockingQueue. If you lot haven’t read them already you lot may break them interesting too useful.
Difference betwixt start too run inward Java Thread
Code Example of start vs run method
Here is a unproblematic code representative which prints cite of Thread which executes run() method of Runnable task. Its clear that if you lot telephone holler upward start() method a novel Thread executes Runnable chore spell if you lot straight telephone holler upward run() method task, electrical flow thread which is principal inward this instance volition execute the task.
public class StartVsRunCall{
public static void main(String args[]) {
//creating 2 threads for start too run method call
Thread startThread = new Thread(new Task("start"));
Thread runThread = new Thread(new Task("run"));
startThread.start(); //calling start method of Thread - volition execute inward novel Thread
runThread.run(); //calling run method of Thread - volition execute inward electrical flow Thread
}
public static void main(String args[]) {
//creating 2 threads for start too run method call
Thread startThread = new Thread(new Task("start"));
Thread runThread = new Thread(new Task("run"));
startThread.start(); //calling start method of Thread - volition execute inward novel Thread
runThread.run(); //calling run method of Thread - volition execute inward electrical flow Thread
}
/*
* Simple Runnable implementation
*/
private static class Task implements Runnable{
private String caller;
public Task(String caller){
this.caller = caller;
}
@Override
public void run() {
System.out.println("Caller: "+ caller + " too code on this Thread is executed yesteryear : " + Thread.currentThread().getName());
}
}
}
Output:
Caller: start too code on this Thread is executed yesteryear : Thread-0
Caller: run too code on this Thread is executed yesteryear : main
Caller: run too code on this Thread is executed yesteryear : main
In Summary alone difference betwixt start() too run() method inward Thread is that start creates novel thread spell run doesn't exercise whatever thread too merely execute inward electrical flow thread similar a normal method call.
Further Learning
Multithreading too Parallel Computing inward Java
Java Concurrency inward Practice - The Book
How to Stop Thread inward Java
Komentar
Posting Komentar