-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadtest.py
More file actions
59 lines (54 loc) · 2 KB
/
threadtest.py
File metadata and controls
59 lines (54 loc) · 2 KB
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
51
52
53
54
55
56
57
58
59
#coding=utf-8
import threading
import time
class scanner(threading.Thread):
tlist=[] #用来存储队列的线程
maxthreads=5 # int(sys.argv[2])最大的并发数量,此处我设置为100,测试下系统最大支持1000多个
evnt=threading.Event()#用事件来让超过最大线程设置的并发程序等待
lck=threading.Lock() #线程锁
def __init__(self, method, *args):
self._method = method
self._args = args
threading.Thread.__init__(self)
def run(self):
try:
self._method(*self._args)
pass
except Exception,e:
print e.message
#以下用来将完成的线程移除线程队列
scanner.lck.acquire()
scanner.tlist.remove(self)
print "rm a t"
#如果移除此完成的队列线程数刚好达到99,则说明有线程在等待执行,那么我们释放event,让等待事件执行
if len(scanner.tlist)==scanner.maxthreads-1:
scanner.evnt.set()
print "to add a t"
scanner.evnt.clear()
print "not add a t"
scanner.lck.release()
@staticmethod
def newthread(method, *args):
scanner.lck.acquire()#上锁
sc=scanner(method, *args)
scanner.tlist.append(sc)
print "add a t"
scanner.lck.release()#解锁
sc.start()
def func(index):
time.sleep(2)
print index
def runscan():
for i in range(10):
scanner.lck.acquire()
#如果目前线程队列超过了设定的上线则等待。
if len(scanner.tlist)>=scanner.maxthreads:
scanner.lck.release()
scanner.evnt.wait()#scanner.evnt.set()遇到set事件则等待结束
else:
scanner.lck.release()
scanner.newthread(func, i)
for t in scanner.tlist:
t.join()#join的操作使得后面的程序等待线程的执行完成才继续
if __name__=="__main__":
runscan()