product = 100
def run1():
global product
for i in range(10000000):
product -= 1
print("consu now",product)
def run2():
global product
for i in range(10000000):
product += 1
print('pro now',product)
if __name__ == "__main__":
c = threading.Thread(target=run1)
p = threading.Thread(target=run2)
c.start()
p.start()
c.join()
p.join()
print(product)
写了if name == "main"他就“同步了”,去掉的话就不同步,什么原理。
在代码示例的这种情况下我写了一千万但还是正确数据
登录后可查看更多问答,登录/注册