如何让多线程的子线程执行完一个再去执行另外一个
for T in range(len(answers)):
submitdata = answers[T]
thread = threading.Thread(target=self.threading_run,args=(T,));
thread.start()
import time, threading
balance = 0lock = threading.Lock()
def change_it(n):
# 先存后取,结果应该为0:
global balance
balance = balance + n
balance = balance - n
def run_thread(n):
for i in range(100000):
# 先要获取锁:
lock.acquire()
try:
# 放心地改吧:
change_it(n)
finally:
# 改完了一定要释放锁:
lock.release()
t1 = threading.Thread(target=run_thread, args=(5,))t2 = threading.Thread(target=run_thread, args=(8,))t1.start()t2.start()t1.join()t2.join()print(balance)为什么锁会报错,而且,我多线程都用不了了
lock = threading.Lock()
AttributeError: module 'threading' has no attribute 'Lock'
1 回答
哆啦的时光机
TA贡献1779条经验 获得超6个赞
用threading.Lock
锁实现线程同步。
另外遍历一个数组请用for item of arr:
遍历,不要用for i in range(len(arr)):
添加回答
举报
0/150
提交
取消