多线程执行同一任务,不共享局部变量
一、 代码展示
import threading
import time
# 多线程执行同一任务时,局部变量是不共享的
def sum_num():
num = 0
i = 1
print("我进来了...")
time.sleep(1)
while i <= 20000000:
num += 1
i += 1
print("%s:%d" % (threading.Thread.name, num))
def main():
t1 = threading.Thread(target=sum_num)
t2 = threading.Thread(target=sum_num)
t1.start()
t2.start()
if __name__ == "__main__":
main()
运行结果
我进来了...
我进来了...
<property object at 0x000002221D4F7E08>:20000000
<property object at 0x000002221D4F7E08>:20000000