-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestSynchronization.java
67 lines (60 loc) · 1.14 KB
/
TestSynchronization.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
60
61
62
63
64
65
66
67
class Table{
synchronized static void printTable(int n){
for(int i=1;i<=10;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){}
}
}
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
Table.printTable(1);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
Table.printTable(10);
}
}
class MyThread3 extends Thread{
Table t;
MyThread3(Table t){
this.t=t;
}
public void run(){
Table.printTable(100);
}
}
class MyThread4 extends Thread{
Table t;
MyThread4(Table t){
this.t=t;
}
public void run(){
Table.printTable(1000);
}
}
public class TestSynchronization {
public static void main(String t[]){
Table obj = new Table();
Table obj1 = new Table();
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
MyThread3 t3=new MyThread3(obj);
MyThread4 t4=new MyThread4(obj);
t1.start();
t2.start();
t3.start();
t4.start();
}
}