-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncTest1BadMain.java
More file actions
39 lines (30 loc) · 896 Bytes
/
Copy pathSyncTest1BadMain.java
File metadata and controls
39 lines (30 loc) · 896 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
package thread.sync.test;
public class SyncTest1BadMain {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Runnable task = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
counter.increment();
}
}
};
Thread thread1 = new Thread(task);
Thread thread2 = new Thread(task);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("결과 : " + counter.getCount());
}
static class Counter {
private int count = 0;
public synchronized void increment() {
count = count + 1;
}
public synchronized int getCount(){
return count;
}
}
}