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