这是一个创建于 2260 天前的主题,其中的信息可能已经有所发展或是发生改变。
Code:
--------------------------------------------------------------
import time
from concurrent.futures import ThreadPoolExecutor
def work_01(i):
time.sleep(1)
print(i)
def work_02(i):
time.sleep(2)
print(i)
def work_03(i):
time.sleep(3)
print(i)
with ThreadPoolExecutor(max_workers=5) as executor:
for i in range(2):
executor.submit(work_01, i)
executor.submit(work_02, i)
executor.submit(work_03, i)
print('over')
-------------------------------------------------------------------
return:
-------------------------------------------------------------------
0
1
0
1
0
1
over
-------------------------------------------------------------------
question:
-------------------------------------------------------------------
1. 在这里提问题怎么上传图片呀?
2. executor.submit(work_01, i) executor.submit(work_02, i) executor.submit(work_03, i)
这三个任务是需要依次顺序执行的,仅在三个任务顺序执行完再一次性打印结果,这有
点类似于 mysql 的组合唯一值,同时我的线程池也是生效的,不会等待 3 个任务执行完再
执行下 3 个任务
3. 理想的展示效果如下:
0
0
0
1
1
1
over
或
1
1
1
0
0
0
over
-------------------------------------------------------------------
6 条回复 • 2018-07-28 08:53:07 +08:00
|
|
1
shangfabao 2018-07-27 11:21:52 +08:00
数量设置为 1,先后添加,然后监听所有线程执行完,java 是这么做的
|
|
|
2
owenliang 2018-07-27 12:00:54 +08:00
了解一下互斥锁+条件变量,构造一个 future,每个线程执行完成后++完成次数,在主线程里 condition wait 等待 3 个都完成,则返回。
|
|
|
3
whoami9894 2018-07-27 12:07:46 +08:00 via Android
六个任务相当于同时开始,sleep(1)的两个最先执行完打印结果
|
|
|
4
liuzhen 2018-07-27 15:25:09 +08:00
给业务上要等待其他线程的所有线程维护一个 countdownlatch ?
|
|
|
5
HackerOO7 2018-07-27 16:05:52 +08:00
线程同步,了解下
|
|
|
6
lolizeppelin 2018-07-28 08:53:07 +08:00 via Android
结果塞到队列里 不用加锁
|