-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiThread.java
59 lines (53 loc) · 1.05 KB
/
MultiThread.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
51
52
53
54
55
56
57
58
59
import java.util.Random;
class Global{
static int num;
}
class ThreadA extends Thread{
public void run(){
Random r = new Random();
for (int i = 0; i < 10; i++){
Global.num = r.nextInt(10);
try{
this.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class ThreadB extends Thread{
public void run(){
for (int i = 0; i < 10; i++){
if(Global.num % 2 == 0)
System.out.println("\n" + Global.num*Global.num);
try{
this.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class ThreadC extends Thread{
public void run(){
for (int i = 0; i < 10; i++){
if(Global.num % 2 != 0)
System.out.println("\n" + Global.num*Global.num*Global.num);
try{
this.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class MultiThread{
public static void main(String args[]){
ThreadA t1 = new ThreadA();
ThreadB t2 = new ThreadB();
ThreadC t3 = new ThreadC();
t1.start();
t2.start();
t3.start();
}
}