线程间通信

#_author:来童星
#date:2019/12/17
from threading import Thread
import time

def plus():
print('子线程1开始')
global num
num+=50
print('num is %d'%num)
print('子线程1结束')
def minus():
print('子线程2开始')
global num
num-=50
print('num is %d'%num)
print('子线程2结束')
num=100
if __name__=='__main__':
print('主线程开始')
print('num is %d' % num)
t1=Thread(target=plus)
t2=Thread(target=minus)
t1.start()
t2.start()
t1.join()
t2.join()
print('主线程结束')
运行结果:
主线程开始
num is 100
子线程1开始
num is 150
子线程1结束
子线程2开始
num is 100
子线程2结束


你可能感兴趣的:(线程间通信)