python 线程锁
""" 线程锁 我这用的是python3.7,必须要加锁才可以保证数据的确定性 """ import time from threading import Thread, Lock lock = Lock() list1 = [0] * 10 def task1(): lock.acquire() # 获取锁 for i in range(len(list1)): list1[i] = 1 print("设置list1") time.sleep(0.5) lock.release() # 释放锁 def task2(): lock.acquire() # 获取锁 for i in range(len(list1)): print("list1[i] =", list1[i]) time.sleep(0.5) lock.release() # 释放锁 if __name__ == ‘__main__‘: t1 = Thread(target=task1, name="task1") t2 = Thread(target=task2, name="task2") t1.start() t2.start() print("over")
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16