-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintinOrder.py
39 lines (30 loc) · 1.18 KB
/
PrintinOrder.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
import threading
from typing import Callable
class Foo:
def __init__(self):
self.firstLock = threading.Lock()
self.secondLock = threading.Lock()
self.firstLock.acquire()
self.secondLock.acquire()
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.firstLock.release()
def second(self, printSecond: 'Callable[[], None]') -> None:
with self.firstLock:
# printSecond() outputs "second". Do not change or remove this line.
printSecond()
self.secondLock.release()
def third(self, printThird: 'Callable[[], None]') -> None:
with self.secondLock:
# printThird() outputs "third". Do not change or remove this line.
printThird()
if __name__ == '__main__':
foo = Foo()
t1 = threading.Thread(target=foo.first, args=(lambda: print("first"),))
t2 = threading.Thread(target=foo.second, args=(lambda: print("second"),))
t3 = threading.Thread(target=foo.third, args=(lambda: print("third"),))
t3.start()
t2.start()
t1.start()
# firstsecondthird