Python 多线程 锁 问题
程序代码:
import threading VALUE = 0 gLOCK = threading.Lock() class MyThread(threading.Thread): def __init__(self, loop_times): super().__init__() self.loop_times = loop_times def run(self): global VALUE for i in range(self.loop_times): gLOCK.acquire() VALUE += 1 gLOCK.release() print(self.loop_times, self.name, VALUE) if __name__ == '__main__': for i in range(2): MyThread(1000000).start()
我想知道为什么这样使用锁得不到期望的预期结果:
1000000 Thread-1 1000000
1000000 Thread-2 2000000