-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynchronizationNon.java
97 lines (77 loc) · 2.01 KB
/
SynchronizationNon.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
public class SynchronizationNon {
static class Table {
synchronized void printTable(String s) {
for (int i = 1; i < 100; i++) {
System.out.printf("Running Thread on %s value: %d%n", s, i);
try {
Thread.sleep(100);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
void modifyTable(String s) {
System.out.println("Modifying data using thread " + s);
}
}
static class Thred1 extends Thread {
Table t;
public Thred1(Table t) {
this.t = t;
}
public void run() {
t.printTable("Thread 1");
}
}
static class Thred2 extends Thread {
Table t;
public Thred2(Table t) {
this.t = t;
}
public void run() {
t.modifyTable("Thread 2");
}
}
static class Thred3 extends Thread {
Table t;
public Thred3(Table t) {
this.t = t;
}
public void run() {
t.printTable("Thread 3");
}
}
static class Thred4 extends Thread {
Table t;
public Thred4(Table t) {
this.t = t;
}
public void run() {
t.printTable("Thread 4");
}
}
static class Thred5 extends Thread {
Table t;
public Thred5(Table t) {
this.t = t;
}
public void run() {
t.printTable("Thread 5");
}
}
static class TestSynchronization {
public static void main(String[] args) {
Table t = new Table();
Thred1 t1 = new Thred1(t);
Thred2 t2 = new Thred2(t);
Thred3 t3 = new Thred3(t);
Thred4 t4 = new Thred4(t);
Thred5 t5 = new Thred5(t);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
}