# -*- coding:utf-8 -*- # -*- coding:utf-8 -*- # 继承式调用 import threading import time class MyThread(threading.Thread): def __init__(self, num,sleep_time): super(MyThread,self).__init__() #threading.Thread.__init__(self) self.num = num self.sleep_time = sleep_time def run(self): # 定义每个线程要运行的函数(必须是run) print("running on number:%s" % self.num) time.sleep(self.sleep_time) print("task done.....",self.num) t1 = MyThread("线程1",2) t2 = MyThread("线程2",5) t1.start() t2.start() t1.join() #等待 # 串行 t2.join() #等待 # 串行 print("主线程") #不加join的时候、主线程和子线程并行,没有依赖关系 #当加上join的时候,主线程依赖子线程执行完毕之后再执行