'''
线程里面的锁
'''
import threading
import time
lock = threading.Lock() # 创建了锁
share_date = 0 # 创建了一个变量
def tstart():
time.sleep(0.1)
global share_date
if lock.acquire(): # 获取了锁
share_date += 1
lock.release() # 释放锁
# tstart(1)
tlist = list() # 创建1个list
for i in range(10):
print(i)
t = threading.Thread(target=tstart, args='')
tlist.append(t)
print(type(t))
print(tlist) # 10个线程
for t in tlist:
t.start()
print("this is main:%s" % time.time())
print("this is main:{}".format(time.time()))
for t in tlist: # 让10个线程都做完
t.join()
print("结束")
print("现在结果:{}".format(share_date))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)