-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPrint in Order.py
56 lines (45 loc) · 1.36 KB
/
Print in Order.py
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
from enum import Enum
from threading import Condition
from time import sleep
from typing import Callable
class State(Enum):
ONE = 1
TWO = 2
THREE = 3
class Foo:
def __init__(self):
self.state = State.ONE
self.cond = Condition()
def first(self, print_first: Callable[[], None]) -> None:
with self.cond:
while self.state != State.ONE:
self.cond.wait()
self.state = State.TWO
self.cond.notifyAll()
print_first()
def second(self, print_second: Callable[[], None]) -> None:
with self.cond:
while self.state != State.TWO:
self.cond.wait()
self.state = State.THREE
self.cond.notifyAll()
print_second()
def third(self, print_third: Callable[[], None]) -> None:
with self.cond:
while self.state != State.THREE:
self.cond.wait()
self.state = State.ONE
self.cond.notifyAll()
print_third()
class Foo:
@staticmethod
def first(print_first: Callable[[], None]) -> None:
print_first()
@staticmethod
def second(print_second: Callable[[], None]) -> None:
sleep(0.05)
print_second()
@staticmethod
def third(print_third: Callable[[], None]) -> None:
sleep(0.1)
print_third()