|
| 1 | +#!/usr/bin/python3 |
| 2 | +""" |
| 3 | +Suppose we have a class: |
| 4 | +
|
| 5 | +public class Foo { |
| 6 | + public void first() { print("first"); } |
| 7 | + public void second() { print("second"); } |
| 8 | + public void third() { print("third"); } |
| 9 | +} |
| 10 | +The same instance of Foo will be passed to three different threads. Thread A |
| 11 | +will call first(), thread B will call second(), and thread C will call third(). |
| 12 | +Design a mechanism and modify the program to ensure that second() is executed |
| 13 | +after first(), and third() is executed after second(). |
| 14 | +
|
| 15 | +
|
| 16 | +
|
| 17 | +Example 1: |
| 18 | +
|
| 19 | +Input: [1,2,3] |
| 20 | +Output: "firstsecondthird" |
| 21 | +Explanation: There are three threads being fired asynchronously. The input |
| 22 | +[1,2,3] means thread A calls first(), thread B calls second(), and thread C |
| 23 | +calls third(). "firstsecondthird" is the correct output. |
| 24 | +Example 2: |
| 25 | +
|
| 26 | +Input: [1,3,2] |
| 27 | +Output: "firstsecondthird" |
| 28 | +Explanation: The input [1,3,2] means thread A calls first(), thread B calls |
| 29 | +third(), and thread C calls second(). "firstsecondthird" is the correct output. |
| 30 | +""" |
| 31 | +fromtypingimportCallable |
| 32 | +fromthreadingimportLock |
| 33 | + |
| 34 | + |
| 35 | +classFoo: |
| 36 | +def__init__(self): |
| 37 | +""" |
| 38 | + Two locks |
| 39 | + """ |
| 40 | +self.locks= [Lock(),Lock()] |
| 41 | +self.locks[0].acquire() |
| 42 | +self.locks[1].acquire() |
| 43 | + |
| 44 | + |
| 45 | +deffirst(self,printFirst:Callable[[],None])->None: |
| 46 | +# printFirst() outputs "first". Do not change or remove this line. |
| 47 | +printFirst() |
| 48 | +self.locks[0].release() |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +defsecond(self,printSecond:Callable[[],None])->None: |
| 53 | +withself.locks[0]: |
| 54 | +# printSecond() outputs "second". Do not change or remove this line. |
| 55 | +printSecond() |
| 56 | +self.locks[1].release() |
| 57 | + |
| 58 | + |
| 59 | +defthird(self,printThird:Callable[[],None])->None: |
| 60 | +withself.locks[1]: |
| 61 | +# printThird() outputs "third". Do not change or remove this line. |
| 62 | +printThird() |
| 63 | + |
| 64 | + |
| 65 | +classFooError: |
| 66 | +def__init__(self): |
| 67 | +""" |
| 68 | + Have a counter, and only the corresponding method can change update the |
| 69 | + counter. |
| 70 | +
|
| 71 | + Error, will miss an input. |
| 72 | + """ |
| 73 | +self._value=1 |
| 74 | +self._lock=Lock() |
| 75 | + |
| 76 | + |
| 77 | +deffirst(self,printFirst:'Callable[[], None]')->None: |
| 78 | +withself._lock: |
| 79 | +ifself._value==1: |
| 80 | +# printFirst() outputs "first". Do not change or remove this line. |
| 81 | +self._value+=1 |
| 82 | +printFirst() |
| 83 | + |
| 84 | + |
| 85 | +defsecond(self,printSecond:'Callable[[], None]')->None: |
| 86 | +withself._lock: |
| 87 | +ifself._value==2: |
| 88 | +# printSecond() outputs "second". Do not change or remove this line. |
| 89 | +self._value+=1 |
| 90 | +printSecond() |
| 91 | + |
| 92 | + |
| 93 | +defthird(self,printThird:'Callable[[], None]')->None: |
| 94 | +withself._lock: |
| 95 | +ifself._value==3: |
| 96 | +# printThird() outputs "third". Do not change or remove this line. |
| 97 | +self._value+=1 |
| 98 | +printThird() |