线程案例
代码展示
import _thread
import time
from concurrent.futures import thread
from threading import Lock
def test(i):
global unfinished_thread
print ('开始运行第%s个进程'%i)
time.sleep(i)
lock.acquire()
unfinished_thread -= 1
print ('结束运行第%s个进程'%i)
lock.release()
# 测试入口
if __name__ == '__main__':
unfinished_thread = 0
# 创建线程锁,用于判断线程是否全部完成
lock = Lock()
start_time = time.time()
for i in range(1, 4, 1):
try:
# 多线程多分类同时运行
unfinished_thread += 1
_thread.start_new_thread(test, (i,))
except:
print ("Error: unable to start thread" + str(i))
while True:
# 等待所有线程完成
lock.acquire()
if unfinish