-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedThreadJoin.java
More file actions
47 lines (39 loc) · 1.16 KB
/
SynchronizedThreadJoin.java
File metadata and controls
47 lines (39 loc) · 1.16 KB
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
class Worker {
private static long count = 0;
public synchronized void increment() {
for (int i = 0; i < 10; i++) {
count++;
}
}
public void run() {
Thread thread1 = new Thread(new Runnable() {
public void run() {
for (long i = 0; i < 10; i++) {
increment();
}
}
});
thread1.start();
Thread thread2 = new Thread(new Runnable() {
public void run() {
for (long i = 0; i < 10; i++) {
increment();
}
}
});
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Count is: " + count);
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
new Worker().run();
System.out.println("Done in: " + (System.currentTimeMillis() - start));
}
}