首页 > 编程语言 > 详细

python 多线程

时间:2018-05-02 14:22:16      阅读:153      评论:0      收藏:0      [点我收藏+]
import threading
import time
from threading import Lock
class Mythread(threading.Thread):#继承与重写多线程
def __init__(self,num):
threading.Thread.__init__(self)
self.num=num
self.lock = Lock()

def run(self):
with self.lock:
self.func()


def func(self):
self.num += 1
time.sleep(1)
self.num -= 1
print(‘I am‘, self.num)




for i in range(100):
mythread = Mythread(0)
mythread.start()
print(mythread.isAlive()) #查看线程存活状态
mythread.join()



class mythread1(threading.Thread):
def __init__(self,threadname):
threading.Thread.__init__(self,name=threadname)
def run(self):
global event
if event.isSet():
event.clear() #清除
event.wait() #等待被设置
j=self.getName()
print(j)
else:
p=self.getName()
print(p)
event.set() #设置
event=threading.Event() #生成事件
event.set() #设置为True
t1=[]
for i in range(10):
t=mythread1(str(i))
t1.append(t)

for i in t1:
i.start()



python 多线程

原文:https://www.cnblogs.com/arrow-kejin/p/8979663.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!