# 创建新线程 for tName in threadList: thread= myThread(threadID, tName, workQueue) thread.start()
threads.append(thread)
threadID +=1
# 填充队列
queueLock.acquire() for word in nameList:
workQueue.put(word)
queueLock.release()
# 等待队列清空 whilenot workQueue.empty(): pass
# 通知线程是时候退出
exitFlag =1
# 等待所有线程完成 for t in threads:
t.join() print("退出主线程")
以上程序执行结果:
开启线程:Thread-1
开启线程:Thread-2
开启线程:Thread-3
Thread-3 processing One
Thread-1 processing Two
Thread-2 processing Three
Thread-3 processing Four
Thread-1 processing Five
退出线程:Thread-3
退出线程:Thread-2
退出线程:Thread-1
退出主线程