import time
import threading
def func_one(name):
for num in range(1,6):
print(f"{name}第{num}次执行")
time.sleep(1)
def func_two(name):
for num in range(1,6):
print(f"{name}第{num}次执行")
time.sleep(1)
if __name__=='__main__':
#创建子线程
t1=threading.Thread(target=func_one,args=("张三",))
t2=threading.Thread(target=func_two,args=("李四",))
t1.start()
t2.start()
print("老板走了")
#类创建的就是线程
import time
from threading import Thread
class DemoOne(Thread):
def __init__(self,name):
super().__init__()
self.name=name
def run(self):
for num in range(1,6):
print(f"{self.name}第{num}次执行")
time.sleep(1)
class DemoTwo(Thread):
def __init__(self,name):
Thread.__init__(self)
self.name=name
def run(self):
for num in range(1,6):
print(f"{self.name}第{num}次执行")
time.sleep(1)
if __name__=='__main__':
t1 = DemoOne("张三")
t2= DemoTwo("李四")
t1.start()
t2.start()
标签:__,name,Thread,self,num,应用,time,多线程
From: https://www.cnblogs.com/tudoot/p/18187481