码迷,mamicode.com
首页 > 编程语言 > 详细

python 多线程

时间:2018-05-02 16:17:38      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:lob   live   lock   with   oba   from   func   设置   事件   

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 多线程

标签:lob   live   lock   with   oba   from   func   设置   事件   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!