目前运行的线程是thread2
thread2—->>>url_4
thread1—->>>url_2
thread2—->>>url_5
thread1—->>>url_3
thread1 ended…
thread2—->>>url_6
thread2 ended…
MainThread ended
2.继承threading.Thread类
import random,threading,time
class MyThread(threading.Thread):
def init(self,name,urls):
threading.Thread.init(self,name=name)
self.urls = urls
def run(self):
print(“目前运行的线程是%s”%threading.current_thread().name)
for url in self.urls:
print(“%s---->>>%s”%(threading.current_thread().name,url,))
time.sleep(random.random())
print(“%s ended”%threading.current_thread().name)
if name == “main”:
print(“目前运行的线程是%s”%threading.current_thread().name)
t1 = MyThread(name&#
本文介绍了Python多线程的概念和实现,通过一个具体的实例展示了如何继承threading.Thread类创建线程。文章还提及作者的背景和一份全面的Python学习资源包,包括学习路线、软件、电子书、视频教程、实战案例和面试资料,旨在帮助Python初学者和进阶者提升技能。
最低0.47元/天 解锁文章

2150

被折叠的 条评论
为什么被折叠?



