自做网站需要多少钱,qq登录网页版登录入口官网,网站建设的专业性对搜索引擎营销的影响,给窗帘做网站本套课在线学习视频#xff08;网盘地址#xff0c;保存到网盘即可免费观看#xff09;#xff1a;
https://pan.quark.cn/s/677661ea63b3
本节将介绍如何利用Python中的thread模块和threading模块实现多线程#xff0c;并通过继承threading.Thread类并重写run方…本套课在线学习视频网盘地址保存到网盘即可免费观看
https://pan.quark.cn/s/677661ea63b3
本节将介绍如何利用Python中的thread模块和threading模块实现多线程并通过继承threading.Thread类并重写run方法的方式创建自定义线程类。我们将以创建唱歌和跳舞两个线程类为例详细讲解如何实现多线程程序。
00:00 - 通过继承实现多线程
创建自定义线程类
我们将创建两个自定义线程类SingThread和DanceThread。这两个类分别实现了线程启动、延迟和结束时的自定义行为并通过传递参数来控制延迟时间。
import threading
import time# 创建唱歌线程类
class SingThread(threading.Thread):def __init__(self, delay):super().__init__()self.delay delaydef run(self):for _ in range(5):print(Singing...)time.sleep(self.delay)# 创建跳舞线程类
class DanceThread(threading.Thread):def __init__(self, delay):super().__init__()self.delay delaydef run(self):for _ in range(5):print(Dancing...)time.sleep(self.delay)# 创建并启动线程
sing_thread SingThread(1)
dance_thread DanceThread(1.5)sing_thread.start()
dance_thread.start()sing_thread.join()
dance_thread.join()print(All threads have finished)
03:53 - Python编程实现多线程
创建多个线程实例
在主程序中通过for循环创建了多个线程实例并启动这些线程。为了确保主线程能够等待所有子线程完成后再继续执行将所有线程放入一个任务列表中并使用for循环调用每个线程的join方法。
import threading
import time# 创建唱歌线程类
class SingThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name nameself.delay delaydef run(self):print(fThread {self.name} started)for _ in range(3):print(f{self.name} is singing...)time.sleep(self.delay)print(fThread {self.name} completed)# 创建跳舞线程类
class DanceThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name nameself.delay delaydef run(self):print(fThread {self.name} started)for _ in range(3):print(f{self.name} is dancing...)time.sleep(self.delay)print(fThread {self.name} completed)# 创建并启动多个线程
threads []
for i in range(3):sing_thread SingThread(fSingThread-{i}, 1 i * 0.5)dance_thread DanceThread(fDanceThread-{i}, 1 i * 0.3)threads.append(sing_thread)threads.append(dance_thread)sing_thread.start()dance_thread.start()# 等待所有线程完成
for thread in threads:thread.join()print(All threads have finished)
计算程序的总运行时间
通过计算程序的开始时间和结束时间的差值可以得到整个程序的运行时间。
import threading
import time# 创建唱歌线程类
class SingThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name nameself.delay delaydef run(self):print(fThread {self.name} started)for _ in range(3):print(f{self.name} is singing...)time.sleep(self.delay)print(fThread {self.name} completed)# 创建跳舞线程类
class DanceThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name nameself.delay delaydef run(self):print(fThread {self.name} started)for _ in range(3):print(f{self.name} is dancing...)time.sleep(self.delay)print(fThread {self.name} completed)# 记录开始时间
start_time time.time()# 创建并启动多个线程
threads []
for i in range(3):sing_thread SingThread(fSingThread-{i}, 1 i * 0.5)dance_thread DanceThread(fDanceThread-{i}, 1 i * 0.3)threads.append(sing_thread)threads.append(dance_thread)sing_thread.start()dance_thread.start()# 等待所有线程完成
for thread in threads:thread.join()# 记录结束时间并计算总运行时间
end_time time.time()
total_time end_time - start_time
print(fAll threads have finished. Total time: {total_time:.2f} seconds)
通过这些示例代码您可以更好地理解如何在Python中使用threading模块创建和管理多线程程序并通过继承threading.Thread类来实现自定义线程类。同时您还可以学习如何计算程序的总运行时间以评估多线程程序的性能。