首页 > 编程语言 > 详细

lock多线程锁

时间:2018-11-18 14:42:43      阅读:103      评论:0      收藏:0      [点我收藏+]

import threading
import time

def run(n):
lock.acquire()
global num
num +=1

lock.release()
time.sleep(1)


lock = threading.Lock()
num = 0
t_objs = [] #存线程实例
for i in range(1000):
t = threading.Thread(target=run,args=("t-%s" %i ,))
t.start()
t_objs.append(t) #为了不阻塞后面线程的启动,不在这里join,先放到一个列表里

for t in t_objs: #循环线程实例列表,等待所有线程执行完毕
t.join()

print("----------all threads has finished...",threading.current_thread(),threading.active_count())

print("num:",num)

lock多线程锁

原文:https://www.cnblogs.com/rongye/p/9977825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!