LeetCode题解(1114):多线程按序打印(Python)
发布日期:2021-06-29 19:55:24 浏览次数:2 分类:技术文章

本文共 768 字,大约阅读时间需要 2 分钟。

题目:(简单)

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

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

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

from threading import Lockclass 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()

转载地址:https://dataartist.blog.csdn.net/article/details/107106358 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(1122):数组的相对排序(Python)
下一篇:LeetCode题解(1108):IP地址无效化(Python)

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月10日 09时45分31秒