【发布时间】:2021-05-30 07:16:59
【问题描述】:
我正在尝试重新创建一个称为睡眠排序的愚蠢想法,但是输出与预期相去甚远。
我期待
0
1
2
3
5
但是我得到了
0
5
5
5
5
...这很奇怪,因为线程会:休眠 (item) 秒,然后打印该项目。
这是我的代码
import threading
import time
def sleepSort(lst):
for item in lst:
threading.Thread(target = lambda: (
time.sleep(item),
print(item)
)).start()
sleepSort([3, 0, 2, 1, 5])
我的代码有问题吗?非常感谢您!
【问题讨论】:
-
我不太清楚你的方法中排序逻辑发生在哪里。
-
@anddt 列表中的每个项目都有自己的线程,它会在其中休眠 (item) 秒。最大的数字休眠时间最长,最后打印,最小的数字最先打印。
标签: python multithreading sorting