Python多线程——递归锁RLOCK
生活随笔
收集整理的這篇文章主要介紹了
Python多线程——递归锁RLOCK
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# with——自動打開自動關閉
import threading
import timeclass Test:rlock=threading.RLock()def __init__(self):self.number=0def execute(self,n):with Test.rlock:self.number+=ndef add(self):with Test.rlock:self.execute(1)def down(self):with Test.rlock:self.execute(-1)def add(test):for i in range(1000000):test.add()
def down(test):for i in range(1000000):test.down()if __name__ == '__main__':t=Test()# args為參數,傳給target所指的函數t1=threading.Thread(target=add,args=(t,))t2=threading.Thread(target=down,args=(t,))t1.start()t2.start()t1.join()t2.join()print(t.number)
?
總結
以上是生活随笔為你收集整理的Python多线程——递归锁RLOCK的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python多线程——LOCK锁
- 下一篇: Python多线程之构建线程池