#多线程---全局变量---共享全局变量 #多线程可以对全局变量进行修改,修改后的结果会影响下一个线程 #进程不可以共享全局变量,子进程是复制父进程的全局变量,修改后互不影响 from threading import Thread import time,random g_num = 100 def work1(): global g_num for i in range(3): g_num += 1 time.sleep(random.random()) print("in work1,g_num=%d"%g_num) def work2(): global g_num for i in range(3): g_num += 1 time.sleep(random.random()) print("in work2,g_num=%d"%g_num) if __name__ == "__main__": # work1() # work2() # print("主线程g_num%d"%g_num) t1 = Thread(target=work1) t2 = Thread(target=work2) t1.start() t2.start() # print("主线程g_num%d" % g_num)