import threading
shared_balance = 0
class Deposit(threading.Thread):
def run(self):
for i in xrange(1000000):
global shared_balance
balance = shared_balance
balance += 100
shared_balance = balance
class Withdraw(threading.Thread):
def run(self):
for i in xrange(1000000):
global shared_balance
balance = shared_balance
balance -= 100
shared_balance = balance
thread1 = Deposit()
thread2 = Withdraw()
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print shared_balance
每次我运行这个程序时,它都会输出一个随机数。如果它存入100、100万次,取出100、100万次,那为什么输出不是0?