赞
踩
展开全部
#下面是一个示例,32313133353236313431303231363533e4b893e5b19e31333337383837我写了一个简单的for循环,并加入了多线程并发。
# -*- coding:utf-8 -*-
import thread,threading
#Test Function
def ForTest():
for i in range(10):
print i
class mythread(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self)
def run(self):
lock.acquire()
for j in xrange(int(times)):
#Add Own Fuction Here
ForTest()
lock.release()
def MutiThread(num,times):
threads=[]
global ft
for x in xrange(num):
threads.append(mythread(num))
for t in threads:
lock.acquire()
t.start()
lock.release()
for t in threads:
t.join()
if __name__ == '__main__':
global num,times,lock
num=2 #num 并发数
times=2 #times 运行次数
lock=threading.Lock()
MutiThread(num,times)
运行结果:
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。