由于线程之间随机调度:某线程可能在执行n条后,CPU接着执行其他线程。为了多个线程同时操作一个内存中的资源时不产生混乱,我们使用锁。
Lock(指令锁)是可用的最低级的同步指令。Lock处于锁定状态时,不被特定的线程拥有。Lock包含两种状态——锁定和非锁定,以及两个基本的方法。
可以认为Lock有一个锁定池,当线程请求锁定时,将线程至于池中,直到获得锁定后出池。池中的线程处于状态图中的同步阻塞状态。
创建锁:
lock=threading.Lock()
cond=threading.Condition(lock=lock)
锁的方法:
cond.acquire(): 获得锁
cond.wait() 等待通知
cond.notify() 通知正在等待的锁
cond.notify_all() 通知所有正在等待的锁
cond.release() 释放锁
实例:
当多线程争夺锁时,允许第一个获得锁的线程进入临街区,并执行代码。
所有之后到达的线程将被阻塞,直到第一个线程执行结束,退出临街区,
并释放锁。需要注意,那些阻塞的线程是没有顺序的。
import threading,time
lista=[]
class huofu(threading.Thread):
def run(self):
while True:
condchi.acquire()
if len(lista)==0:
for i in range(1,11):
lista.append(i)
print("正在生产第{}个馒头".format(i))
time.sleep(1)
condchi.notify_all()
condchi.release()
class chihuo(threading.Thread):
def __init__(self,name):
threading.Thread.__init__(self)
self.name=name
def run(self):
mantou=[]
while True:
condchi.acquire()
if len(lista)>0:
mantou=lista.pop()
time.sleep(1)
else:
condhuo.acquire()
condhuo.notify()
condhuo.release()
condchi.wait()
condchi.release()
if mantou not in lista:
print("{}在吃第{}个馒头".format(self.name,mantou))
lock1=threading.Lock()
condhuo=threading.Condition(lock1)
lock2=threading.Lock()
condchi=threading.Condition(lock2)
huofu1=huofu()
chihuo1=chihuo("handao")
chihuo2=chihuo("tanzhenghua")
chihuo3=chihuo("laowang")
huofu1.start()
chihuo1.start()
chihuo2.start()
chihuo3.start()
相关知识
Python笔试题
线程池提交任务的两种方式:execute与submit的区别
Python期末作业
国运:扮演李逍遥,队友东方镜
解决多线程中资源竞争
高并发之——死锁,死锁的四个必要条件以及处理策略
Python小练习
python运行run在哪
python 练习题
python学习总结day2
网址: python 线程锁 https://m.mcbbbk.com/newsview285788.html
上一篇: 宇宙资源争夺战小游戏,在线玩,4 |
下一篇: java多线程面试题整理及答案 |