import threading
import time
#继承 class threading.Thread
class MyThread(threading.Thread):
#类做初始化
def __init__(self,name):
#调用hreading.Thread.__init__(self)方法
threading.Thread.__init__(self)
self.name=name
# 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
def run(self):
print_time(self.name)
def print_time(name):
if name=="Thread1":
for i in range(3):
print("Thread1 Star"+time.ctime(time.time()))
time.sleep(2)
print("Thread1"+time.ctime(time.time()))
print("Thread1 End" + time.ctime(time.time()))
else:
for i in range(3):
print("Thread2 Star"+time.ctime(time.time()))
time.sleep(5)
print("Thread2"+time.ctime(time.time()))
print("Thread2 End" + time.ctime(time.time()))
#创建线程
t1=MyThread("Thread1")
t2=MyThread("Thread2")
#启动线程
t1.start()
t2.start()
#守护进程,直到子线程结束
t1.join()
t2.join()