1.锁: lock (1次放1个)

2.锁: Rlock (1次放1个)

3.锁: BoundedSemaphore() (1次放N个,默认1个) 信号量

4.锁: Condition() (1次放任意个数,动态的)

#############第一种方法#############
import threading

lock = threading.Condition()

def func(arg):
lock.acquire()
lock.wait()
print(arg)
lock.release()

for i in range(10):
t = threading.Thread(target=func, args=(i,))
t.start()

while True:
imp = int(input())
lock.acquire()
lock.notify(imp)
lock.release()
#####################################

##############第二种方法##############
import threading

lock = threading.Condition()

def func1():
print(‘来执行函数了’)
input(’>>>>’)
return True

def func(arg):
print(‘线程来了’)
lock.wait_for(func1)
print(arg)

for i in range(10):
t = threading.Thread(target=func, args=(i,))
t.start()

######################################

上一篇:vue-vuex-actions的基本使用


下一篇:AQS-ReentrantLock.CountDownLatch 源码