-
Notifications
You must be signed in to change notification settings - Fork 0
/
Synchronization.java
51 lines (41 loc) · 1.04 KB
/
Synchronization.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
public class Synchronization {
static class Table {
synchronized void printTable(int n) {
for (int i = 1; i < 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
static class Thred1 extends Thread {
Table t;
public Thred1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}
}
static class Thred2 extends Thread {
Table t;
public Thred2(Table t) {
this.t = t;
}
public void run() {
t.printTable(100);
}
}
static class TestSynchronization {
public static void main(String[] args) {
Table t = new Table();
Thred1 t1 = new Thred1(t);
Thred2 t2 = new Thred2(t);
t1.start();
t2.start();
}
}
}