Discuss / Python / 运行10亿次出现的结果之一

运行10亿次出现的结果之一

Topic source

GOGOGO-枼子

#1 Created at ... [Delete] [Delete and Lock User]

import time, threading

假定这是你的银行存款

balance = 0

def change_it(n):

#先存后取,结果应该为0:
global balance
balance = balance + n
balance = balance - n

def run_thrad(n): for i in range(1000000000): change_it(n)

t1 = threading.Thread(target=run_thrad, args=(5,)) t2 = threading.Thread(target=run_thrad,args=(8,)) t1.start() t2.start() t1.join() t2.join() print(balance)

结果: 7257

发家致富

happyflycs神

#3 Created at ... [Delete] [Delete and Lock User]

我不管怎么运行最后都是0,想出错都错不了。加几个sleep中间输出有几个5,8的,但最后一定是0.


  • 1

Reply