import threading
x = 0;
class Thread1(threading.Thread):
def run(self):
global x
for i in range(1,100000):
x = x + 1
class Thread2(threading.Thread):
def run(self):
global x
for i in range(1,100000):
x = x - 1
#create two threads
t1 = Thread1()
t2 = Thread2()
#start the threads
t1.start()
t2.start()
#wait for the threads to finish
t1.join()
t2.join()
print x;
Running this multiple times produces different output, some in the negative and some in the positive. Is it because the two threads are using the same global x? I don't understand why: shouldn't the net effect (output) be the same after all the dust settles?