-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStartVsRunCall.java
More file actions
41 lines (31 loc) · 1.21 KB
/
StartVsRunCall.java
File metadata and controls
41 lines (31 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Difference between start and run in Java Thread
http://javarevisited.blogspot.com/2012/03/difference-between-start-and-run-method.html#ixzz4aZdrgfBs
Expected output:
Caller: start and code on this Thread is executed by : Thread-0
Caller: run and code on this Thread is executed by : main
*/
public class StartVsRunCall {
public static void main(String args[]) {
//creating two threads for start and run method call
Thread startThread = new Thread(new Task("start"));
Thread runThread = new Thread(new Task("run"));
startThread.start(); //calling start method of Thread - will execute in new Thread
runThread.run(); //calling run method of Thread - will execute in current 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
+ " and code on this Thread is executed by : "
+ Thread.currentThread().getName());
}
}
}