1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import threading,time
# sum=[]
lock = threading.Lock()
from Queue import Queue
maxs=5 ##并发的线程数量
threadLimiter=threading.BoundedSemaphore(maxs)
class test(threading.Thread):
def __init__(self,q,p):
self.p = p
self.q = q
threading.Thread.__init__(self)
def run(self):
threadLimiter.acquire() #获取
# global sum
# lock.acquire()
# sum.append(self.p)
try:
self.q.put(self.p)
except:
pass
finally:
threadLimiter.release() #释放
# time.sleep(0.5)
# lock.release()
def get_result(self):
return self.q
def tt(x):
queue = Queue()
for i in range(10):
cur=test(queue,x)
cur.start()
for i in range(10):
cur.join()
while cur.isAlive():
continue
out=[]
tmp=cur.get_result()
while not tmp.empty():
out.append(tmp.get())
return out
print tt(11)
print tt(12)