线程锁的实际应用
效果:只有条件A.sumtol为真时才能执行。首次是真进入执行。之后再次创建对象不再进入该+1打印的分支内
import threading
class A:
sumtol = True
def __init__(self):
lock = threading.Lock()
lock.acquire()
if A.sumtol:
print("+1=", A.sumtol)
A.sumtol=False
lock.release()
def print_sumtol(self):
print("comming...", A.sumtol)
print(A.sumtol)
def print_end(self):
print("end=", A.sumtol)
def func1():
ax = A()
ax.print_sumtol()
def func2():
ax = A()
ax.print_end()
t1 = threading.Thread(target=func1)
t1.start()
t2 = threading.Thread(target=func2)
t2.start()
t1.join()
t2.join()