python3 线程 threading
最基础的线程的使用
import threading, time
value = 0
lock = threading.Lock()
def change(n):
global value
value += n
value -= n
def thread_fun(n):
for i in range(1000):
lock.acquire()
try:
change(n)
finally:
lock.release()
t1 = threading.Thread(target=thread_fun, args=(2,))
t2 = threading.Thread(target=thread_fun, args=(3,))
t1.start()
t2.start()
t1.join()
t2.join()
print(value)
output: 0
import th