LeetCode题解(1114):多线程按序打印(Python)

    技术2023-08-04  80

    题目:原题链接(简单)

    解法时间复杂度空间复杂度执行用时Ans 1 (Python)––52ms (66.97%)Ans 2 (Python)Ans 3 (Python)

    LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。

    解法一(使用threading.Lock实现):

    from threading import Lock class Foo: def __init__(self): self.firstJobDone = Lock() self.secondJobDone = Lock() self.firstJobDone.acquire() self.secondJobDone.acquire() def first(self, printFirst: 'Callable[[], None]') -> None: printFirst() self.firstJobDone.release() def second(self, printSecond: 'Callable[[], None]') -> None: with self.firstJobDone: printSecond() self.secondJobDone.release() def third(self, printThird: 'Callable[[], None]') -> None: with self.secondJobDone: printThird()
    Processed: 0.011, SQL: 9