Python:线程同步
锁定代码块:
threadLock = threading.Lock()
threadLock.acquire()
# somecode...
threadLock.release()
阻塞直到线程结束:
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
thread1.start()
thread2.start()
# 添加线程到线程列表
threads.append(thread1)
threads.append(thread2)
# 等待所有线程完成
for t in threads:
t.join()
print "Exiting Main Thread"
join([time])
方法: 等待至线程中止。
这阻塞调用线程直至线程的join()
方法被调用中止:
- 正常退出
- 抛出未处理的异常
- 可选的超时发生。