线程类主动创建线程对象
import threadingimport timedef listen(): print("listen") time.sleep(3) print("listen over")def writeblog(): print("writeblog") time.sleep(5) print("writeblog over") print(time.time()-s)s=time.time()# 线程对象t1=threading.Thread(target=listen)t2=threading.Thread(target=writeblog)t1.start()t2.start()print("ending")
继承线程类创建对象
#继承Thread式创建import threadingimport timeclass MyThread(threading.Thread): def __init__(self,num): threading.Thread.__init__(self) self.num=num def run(self): print("running on number:%s" %self.num) # 睡三秒程序结束 time.sleep(3)t1=MyThread(56)t2=MyThread(78)t1.start()t2.start()print("ending")