简单的自创线程池
简单的线程池
import threading
import time
import queue
class ThreadPool(object):
def __init__(self,max_num=20):
self.queue = queue.Queue(max_num)
for i in range(max_num):
self.queue.put(threading.Thread)
def get_thread(self):
return self.queue.get()
def add_thread(self):
self.queue.put(threading.Thread)
def func(pool,a1):
time.sleep(1)
print(a1)
pool.add_thread()
p =ThreadPool(10)
for i in range(100):
thread = p.get_thread()
t =thread(target=func,args=(p,i,))
t.start()