[Python] 纯文本查看 复制代码
import threading
import queue
from time import sleep
class Mythread(threading.Thread):
def __init__(self,que):
threading.Thread.__init__(self)
self.queue = que
def run(self):
while True:
sleep(1)
if self.queue.empty():
break
item = self.queue.get()
print(item,'!')
self.queue.task_done()
return
que = queue.Queue()
tasks = [Mythread(que) for x in range(1)]
for x in range(10):
que.put(x)
for x in tasks:
t = Mythread(que)
t.start()
que.join()
[Python] 纯文本查看 复制代码
import threading
import queue
from time import sleep
class Mythread(threading.Thread):
def __init__(self,que):
threading.Thread.__init__(self)
self.queue = que
def run(self):
while True:
item = self.queue.get()
self.queue.task_done()
if item == None:
break
print(item,'!')
return
que = queue.Queue()
tasks = [Mythread(que) for x in range(1)]
for x in tasks:
t = Mythread(que)
t.start()
for x in range(10):
sleep(1)
que.put(x)
for x in tasks:
que.put(None)