1114. 按序打印
大约 2 分钟
---
1114. 按序打印
- 标签:多线程
- 难度:简单
题目链接
题目大意
描述:三个不同的线程 A、B、C 会共用一个 Foo 实例,分别调用 first()、second() 和 third() 方法。三个线程是异步启动的,我们无法控制操作系统调度线程的顺序。
要求:确保 second() 在 first() 之后执行,third() 在 second() 之后执行。最终输出为 "firstsecondthird"。
说明:
- 尽管输入中的数字表示线程的启动顺序,但实际调度顺序是不确定的。
示例:
- 示例 1:
输入:nums = [1,2,3]
输出:"firstsecondthird"- 示例 2:
输入:nums = [1,3,2]
输出:"firstsecondthird"解题思路
思路 1:锁(Lock)
拆解步骤:
创建两个锁:
lock_second:控制second能否执行,初始为锁定状态(关着门)lock_third:控制third能否执行,初始为锁定状态(关着门)
first()执行完后:打开lock_second的门,允许second()执行。second()执行前:先等lock_second开门(即等first完成),执行完后打开lock_third的门,允许third()执行。third()执行前:先等lock_third开门(即等second完成),然后执行。
思路 1:代码
from threading import Lock
class Foo:
def __init__(self):
# 创建两个锁,初始都是锁定状态
self.lock_second = Lock()
self.lock_third = Lock()
self.lock_second.acquire() # second 的门关着
self.lock_third.acquire() # third 的门关着
def first(self, printFirst: 'Callable[[], None]') -> None:
# printFirst() outputs "first". Do not change or remove this line.
printFirst()
self.lock_second.release() # second 可以执行了
def second(self, printSecond: 'Callable[[], None]') -> None:
with self.lock_second: # 等 first 执行完
# printSecond() outputs "second". Do not change or remove this line.
printSecond()
self.lock_third.release() # third 可以执行了
def third(self, printThird: 'Callable[[], None]') -> None:
with self.lock_third: # 等 second 执行完
# printThird() outputs "third". Do not change or remove this line.
printThird()思路 1:复杂度分析
- 时间复杂度:。每个方法只执行一次,没有循环。
- 空间复杂度:。只用了两个锁,常数空间。