-
有传入值以及有返回值的多线程
-
- 代码实例
# -*- coding:utf-8 -*-
# 线程使用的方式一
from threading import Thread
import time
# 需要多线程运行的函数
def sum_num(b_data,e_data):
num = b_data+e_data
return num
class MyThread(Thread):
def __init__(self, b_data,e_data):
Thread.__init__(self)
self.b_data = b_data
self.e_data = e_data
def run(self):
self.result = sum_num(self.b_data,self.e_data)
def get_result(self):
return self.result
# 创建线程
if __name__ == '__main__':
thd1 = MyThread(601,620)
thd2 = MyThread(620,630)
start_time = time.time()
thd1.start()
thd2.start()
thd1.join()
thd2.join()
end_time = time.time()
print("两线程一共的运行时间为:", end_time-start_time)
print("主线程结束")
print(thd1.get_result())
print(thd2.get_result())
-
- 运行结果
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/tech/python/275661.html