import threading
import time
import queue
class MyThread(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.Name = name
def run(self):
print('开启的线程是:', self.Name)
while not workQueue.empty():
index = workQueue.get()
nowTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print('%s - %s线程正在执行第%d个任务' % (nowTime, self.Name, index))
print('结束的线程是:', self.Name)
numList = [i for i in range(0, 100)]
workQueue = queue.Queue(len(numList))
if __name__ == '__main__':
for num in numList:
workQueue.put(num)
threads = []
threadsName = ['一号线程', '二号线程', '三号线程']
for tName in threadsName:
thread = MyThread(tName)
thread.start()
threads.append(thread)
for i in threads:
i.join()
print('全部执行完成!')