# 递归锁:就是一把锁中还有一把小锁,比如学校的大门口有一个大锁,学校里的 #每个教室也有一把小锁,以后所有的锁都用rlock就可以了,不要用lock,尤其是多层锁的时候,必须要用递归锁 import threading import time def run1(): print("grab the first part data") lock.acquire() global num1 num1 += 1 lock.release() return num1 def run2(): print("grab the second part data") lock.acquire() global num2 num2 += 1 lock.release() return num2 def run3(): lock.acquire() res1 = run1() print("--------between run1 and run2-------") res2 = run2() lock.release() print("函数run1--->",res1,"函数run2--->",res2) if __name__ == '__main__': num1 = 0 num2 = 0 lock = threading.RLock() for i in range(5): t = threading.Thread(target=run3) t.start() while threading.active_count() != 1: print("剩余的线程数;[%s]" %threading.active_count()) else: print("all thread is down") print(num1,num2)