-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
53 lines (50 loc) · 1.48 KB
/
MainClass.java
File metadata and controls
53 lines (50 loc) · 1.48 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
48
49
50
51
52
53
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MainClass {
private static int flag = 0;
public static void main(String[] args) throws Exception {
final Lock lock = new ReentrantLock();
Thread a = new Thread(new Runnable() {//
@Override
public void run() {
while (flag <= 30) {
lock.lock();
if (flag % 3 == 0) {
System.out.println("A");
flag ++;
}
lock.unlock();
}
}
});
Thread b = new Thread(new Runnable() {
@Override
public void run() {
while (flag <= 30) {
lock.lock();
if (flag % 3 == 1) {
System.out.println("B");
flag ++;
}
lock.unlock();
}
}
});
Thread c = new Thread(new Runnable() {
@Override
public void run() {
while (flag <= 30) {
lock.lock();
if (flag % 3 == 2) {
System.out.println("C");
flag ++;
}
lock.unlock();
}
}
});
a.start();
b.start();
c.start();
}
}