|
| 1 | +//https://leetcode.com/problems/print-in-order/discuss/335939/5-Python-threading-solutions-(Barrier-Lock-Event-Semaphore-Condition)-with-explanation |
| 2 | + |
| 3 | +//Runtime: 28 ms, faster than 96.04% of Python3 online submissions for Print in Order. |
| 4 | +//Memory Usage: 13.1 MB, less than 100.00% of Python3 online submissions for Print in Order. |
| 5 | + |
| 6 | +from threading import Barrier |
| 7 | + |
| 8 | +class Foo: |
| 9 | + def __init__(self): |
| 10 | + self.first_barrier = Barrier(2) |
| 11 | + self.second_barrier = Barrier(2) |
| 12 | + |
| 13 | + def first(self, printFirst: 'Callable[[], None]') -> None: |
| 14 | + |
| 15 | + # printFirst() outputs "first". Do not change or remove this line. |
| 16 | + printFirst() |
| 17 | + self.first_barrier.wait() |
| 18 | + |
| 19 | + |
| 20 | + def second(self, printSecond: 'Callable[[], None]') -> None: |
| 21 | + self.first_barrier.wait() |
| 22 | + # printSecond() outputs "second". Do not change or remove this line. |
| 23 | + printSecond() |
| 24 | + self.second_barrier.wait() |
| 25 | + |
| 26 | + |
| 27 | + def third(self, printThird: 'Callable[[], None]') -> None: |
| 28 | + self.second_barrier.wait() |
| 29 | + # printThird() outputs "third". Do not change or remove this line. |
| 30 | + printThird() |
| 31 | + |
| 32 | +//Runtime: 40 ms, faster than 59.25% of Python3 online submissions for Print in Order. |
| 33 | +//Memory Usage: 13.1 MB, less than 100.00% of Python3 online submissions for Print in Order. |
| 34 | + |
| 35 | +from threading import Lock |
| 36 | + |
| 37 | +class Foo: |
| 38 | + def __init__(self): |
| 39 | + self.locks = (Lock(), Lock()) |
| 40 | + self.locks[0].acquire() |
| 41 | + self.locks[1].acquire() |
| 42 | + |
| 43 | + def first(self, printFirst: 'Callable[[], None]') -> None: |
| 44 | + |
| 45 | + # printFirst() outputs "first". Do not change or remove this line. |
| 46 | + printFirst() |
| 47 | + self.locks[0].release() |
| 48 | + |
| 49 | + def second(self, printSecond: 'Callable[[], None]') -> None: |
| 50 | + |
| 51 | + # printSecond() outputs "second". Do not change or remove this line. |
| 52 | + with self.locks[0]: |
| 53 | + printSecond() |
| 54 | + self.locks[1].release() |
| 55 | + |
| 56 | + |
| 57 | + def third(self, printThird: 'Callable[[], None]') -> None: |
| 58 | + |
| 59 | + # printThird() outputs "third". Do not change or remove this line. |
| 60 | + with self.locks[1]: |
| 61 | + printThird() |
| 62 | + |
| 63 | +//Runtime: 40 ms, faster than 59.25% of Python3 online submissions for Print in Order. |
| 64 | +//Memory Usage: 13.1 MB, less than 100.00% of Python3 online submissions for Print in Order. |
| 65 | + |
| 66 | +from threading import Event |
| 67 | + |
| 68 | +class Foo: |
| 69 | + def __init__(self): |
| 70 | + self.done = (Event(), Event()) |
| 71 | + |
| 72 | + def first(self, printFirst: 'Callable[[], None]') -> None: |
| 73 | + |
| 74 | + # printFirst() outputs "first". Do not change or remove this line. |
| 75 | + printFirst() |
| 76 | + self.done[0].set() |
| 77 | + |
| 78 | + |
| 79 | + def second(self, printSecond: 'Callable[[], None]') -> None: |
| 80 | + |
| 81 | + # printSecond() outputs "second". Do not change or remove this line. |
| 82 | + self.done[0].wait() |
| 83 | + printSecond() |
| 84 | + self.done[1].set() |
| 85 | + |
| 86 | + |
| 87 | + def third(self, printThird: 'Callable[[], None]') -> None: |
| 88 | + |
| 89 | + # printThird() outputs "third". Do not change or remove this line. |
| 90 | + self.done[1].wait() |
| 91 | + printThird() |
| 92 | + |
| 93 | +/Runtime: 44 ms, faster than 44.53% of Python3 online submissions for Print in Order. |
| 94 | +//Memory Usage: 13 MB, less than 100.00% of Python3 online submissions for Print in Order. |
| 95 | + |
| 96 | +from threading import Semaphore |
| 97 | + |
| 98 | +class Foo: |
| 99 | + def __init__(self): |
| 100 | + self.gates = (Semaphore(0), Semaphore(0)) |
| 101 | + |
| 102 | + |
| 103 | + def first(self, printFirst: 'Callable[[], None]') -> None: |
| 104 | + |
| 105 | + # printFirst() outputs "first". Do not change or remove this line. |
| 106 | + printFirst() |
| 107 | + self.gates[0].release() |
| 108 | + |
| 109 | + |
| 110 | + def second(self, printSecond: 'Callable[[], None]') -> None: |
| 111 | + |
| 112 | + # printSecond() outputs "second". Do not change or remove this line. |
| 113 | + with self.gates[0]: |
| 114 | + printSecond() |
| 115 | + self.gates[1].release() |
| 116 | + |
| 117 | + |
| 118 | + def third(self, printThird: 'Callable[[], None]') -> None: |
| 119 | + |
| 120 | + # printThird() outputs "third". Do not change or remove this line. |
| 121 | + with self.gates[1]: |
| 122 | + printThird() |
| 123 | + |
| 124 | +//Runtime: 44 ms, faster than 44.53% of Python3 online submissions for Print in Order. |
| 125 | +//Memory Usage: 13 MB, less than 100.00% of Python3 online submissions for Print in Order. |
| 126 | + |
| 127 | +from threading import Condition |
| 128 | + |
| 129 | +class Foo: |
| 130 | + def __init__(self): |
| 131 | + self.exec_condition = Condition() |
| 132 | + self.order = 0 |
| 133 | + self.first_finish = lambda : self.order == 1 |
| 134 | + self.second_finish = lambda : self.order == 2 |
| 135 | + |
| 136 | + def first(self, printFirst: 'Callable[[], None]') -> None: |
| 137 | + |
| 138 | + # printFirst() outputs "first". Do not change or remove this line. |
| 139 | + with self.exec_condition: |
| 140 | + printFirst() |
| 141 | + self.order = 1 |
| 142 | + self.exec_condition.notify(2) |
| 143 | + |
| 144 | + |
| 145 | + def second(self, printSecond: 'Callable[[], None]') -> None: |
| 146 | + |
| 147 | + # printSecond() outputs "second". Do not change or remove this line. |
| 148 | + with self.exec_condition: |
| 149 | + self.exec_condition.wait_for(self.first_finish) |
| 150 | + printSecond() |
| 151 | + self.order = 2 |
| 152 | + self.exec_condition.notify() |
| 153 | + |
| 154 | + |
| 155 | + def third(self, printThird: 'Callable[[], None]') -> None: |
| 156 | + |
| 157 | + # printThird() outputs "third". Do not change or remove this line. |
| 158 | + with self.exec_condition: |
| 159 | + self.exec_condition.wait_for(self.second_finish) |
| 160 | + printThird() |
0 commit comments