-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadIndependence.java
50 lines (43 loc) · 1.3 KB
/
ThreadIndependence.java
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
42
43
44
45
46
47
48
49
50
package com.basic.threading;
/**
*
* @author hp
*/
class IndependentThread extends Thread{
public void run(){
for(int i=1; i<=5; i++){
System.out.printf("Independent Thread %d\n",i);
}
}
}
class IndependentRunner implements Runnable{
public void run(){
for(int i=1; i<=5; i++){
System.out.printf("Independent Runner %d\n",i);
}
}
}
public class ThreadIndependence {
public static void main(String[] args) {
/* all print operation will execute randomly according with processor
* and system kernel
* Thread:1 print operation will always execute first
* next operation will randomly execute
*/
/* Calling Independent Thread which is Thread extended */
IndependentThread t1 = new IndependentThread();
t1.start();
/* Creating a Thread for Runner Runnable */
Thread t2 = new Thread(new IndependentRunner());
t2.start();
/* Creating a anonymous Runnable Thread */
Thread t3 = new Thread(new Runnable(){
public void run(){
for(int i=1; i<=5; i++){
System.out.printf("Anonymous Runner %d\n",i);
}
}
});
t3.start();
}
}