-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintInOrder.java
52 lines (41 loc) · 1.26 KB
/
PrintInOrder.java
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
package question;
/**
* question
* LeetCode
* 2020.09.06.下午1:31
* * <p>
* 1114. Print in Order
* https://leetcode.com/problems/print-in-order/
*
* @author : rick
*/
public class PrintInOrder {
class Foo {
volatile int lock = 0;
int first = 1<< 1;
int second = 1<< 2;
int third = 1<< 3;
public Foo() {
}
public void first(Runnable printFirst) throws InterruptedException {
while(lock == 0){
// printFirst.run() outputs "first". Do not change or remove this line.
printFirst.run();
lock = lock | first;
}
}
public void second(Runnable printSecond) throws InterruptedException {
while((lock & first) == 0)
Thread.sleep(2);
// printSecond.run() outputs "second". Do not change or remove this line.
printSecond.run();
lock = lock | second;
}
public void third(Runnable printThird) throws InterruptedException {
while((lock & first) == 0 || (lock & second) == 0)
Thread.sleep(3);
// printThird.run() outputs "third". Do not change or remove this line.
printThird.run();
}
}
}