import threading
import time
num = 0
lock = threading.Lock()
def task1(x):
global num
lock.acquire()
for i in range(x):
num += 1
print("task1","task1","task1")
lock.release()
# time.sleep(3)
def task2(x):
global num
with lock:
for i in range(x):
num -= 1
print("task2","task2","task2")
# time.sleep(3)
def main():
t1 = threading.Thread(target=task1, args=(100,))
t2 = threading.Thread(target=task2, args=(100,))
t1.start()
t2.start()
t1.join()
t2.join()
print("結(jié)果:", num)
if __name__ == '__main__':
main()
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。