import threading import time init_total = 0 lock = threading.Lock() def test_01(): global init_total lock.acquire() for i in range(100000): init_total += 1 lock.release() def test_02(): global init_total lock.acquire() for i in range(100000): init_total += 1 lock.release() def main(): t_01 = threading.Thread(target=test_01) t_01.start() t_02 = threading.Thread(target=test_02) t_02.start() time.sleep(1) print(init_total) # 多线程 if __name__ == '__main__': main()