-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncWord.java
More file actions
48 lines (38 loc) · 795 Bytes
/
SyncWord.java
File metadata and controls
48 lines (38 loc) · 795 Bytes
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
package examples2;
public class SyncWord {
private int count = 0;
public synchronized void increment() {
count++;
}
public static void main(String[] args) {
SyncWord s = new SyncWord();
s.doWork();
}
public void doWork() {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
increment();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100000; i++) {
increment();
}
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Final Count: " + count);
}
}