|
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 |
import
threadingimport
timeclass
Mythread(threading.Thread): def
run(self): global
num if
mutex.acquire(): num+=1 print
num mutex.release()num=0mutex=threading.Lock()def
test(): for
i in
range(5): t=Mythread() t.start()if __name__ ==
‘__main__‘: test() ‘‘‘class mythread(threading.Thread): def __init__(self,num): threading.Thread.__init__(self) self.num=num def run(self): print self.numfor i in range(5): t=mythread(i) t.start()#t=threading.Thread(target=run,args=(15,20))#t.start() ‘‘‘ |
原文:http://www.cnblogs.com/huangxiaohen/p/3627994.html