python threading.thread
时间: 2023-09-15 21:19:21 浏览: 112
Python的threading.Thread类表示在单独的控制线程中运行的活动。有两种方法可以指定这种活动:第一种方法是通过给构造函数传递回调对象来指定活动,例如:
mthread=threading.Thread(target=xxxx,args=(xxxx))
mthread.start()
第二种方法是在子类中重写run()方法,例如:
import threading, time
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global n, lock
time.sleep(1)
if lock.acquire():
print n , self.name
n = 1
lock.release()
if "__main__" == __name__:
n = 1
ThreadList = []
lock = threading.Lock()
for i in range(1, 200):
t = MyThread()
ThreadList.append(t)
for t in ThreadList:
t.start()
for t in ThreadList:
t.join()
所以,Python的threading.Thread类可以通过传递回调对象或在子类中重写run()方法来指定活动。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
阅读全文