forked from slgobinath/Java-Helps-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrencyProblem.java
More file actions
35 lines (30 loc) · 791 Bytes
/
ConcurrencyProblem.java
File metadata and controls
35 lines (30 loc) · 791 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
public class ConcurrencyProblem {
static int[] array = {0};
public static void main(String[] args) throws InterruptedException {
Thread a = new Thread() {
public void run() {
for(int i = 1; i <= 1000; i++) {
increase();
}
}
};
Thread b = new Thread() {
public void run() {
for(int i = 1; i <= 1000; i++) {
decrease();
}
}
};
a.start();
b.start();
a.join();
b.join();
System.out.println(array[0]);
}
public synchronized static void increase() {
array[0]++;
}
public synchronized static void decrease() {
array[0]--;
}
}