在多线程开发中,全局变量是多个线程都共享的数据,而局部变量等是各自线程的,是非共享的。
from threading import Thread
import time
classMyThread(Thread):
# 重写 构造方法
def __init__(self, num):
# Thread.__init__(self)
super(MyThread, self).__init__()
self.num = num
def run(self):
self.num += 1
print('线程(%s),num=%d' % (self.name, self.num))
time.sleep(0.1)
self.num += 1
print('线程(%s),num=%d' % (self.name, self.num))
if __name__ =='__main__':
t1 = MyThread(100)
t2 = MyThread(200)
t1.start()
t2.start()
t1.join()
t2.join()