-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1114.cs
60 lines (54 loc) · 1.35 KB
/
Solution1114.cs
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
54
55
56
57
58
59
60
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1114
{
/// <summary>
/// 1114. Print in Order - Easy
/// <a href="https://leetcode.com/problems/print-in-order">See the problem</a>
/// </summary>
public class Foo
{
private readonly object _lock = new();
private bool _firstPrinted;
private bool _secondPrinted;
public Foo()
{
_firstPrinted = false;
_secondPrinted = false;
}
public void First(Action printFirst)
{
lock (_lock)
{
printFirst();
_firstPrinted = true;
Monitor.PulseAll(_lock);
}
}
public void Second(Action printSecond)
{
lock (_lock)
{
while (!_firstPrinted)
{
Monitor.Wait(_lock);
}
printSecond();
_secondPrinted = true;
Monitor.PulseAll(_lock);
}
}
public void Third(Action printThird)
{
lock (_lock)
{
while (!_secondPrinted)
{
Monitor.Wait(_lock);
}
printThird();
}
}
}
}