# -*-coding:utf-8-*-
import time
from time import ctime, sleep
import threading
import collections
class MyThread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def run(self):
self.result = self.func(*self.args)
def get_result(self):
try:
return self.result
except Exception:
return None
def loop(nloop):
print('start_task_time', ctime())
time.sleep(3)
print('end_task_time', ctime())
return nloop
def main():
print('start at', ctime())
threads = []
nloops = range(5)
for i in nloops:
t = MyThread(loop, (i,), loop.__name__)
threads.append(t)
for i in nloops: # start threads 此处并不会执行线程,而是将任务分发到每个线程,同步线程。等同步完成后再开始执行start方法
threads[i].start()
for i in nloops: # jion()方法等待线程完成
threads[i].join()
print(threads[1].get_result())
print('DONE AT:', ctime())
if __name__ == '__main__':
main()