这是自定义多线程吗?做完自己不确定是不是多线程
import threadingimport time
class MyThread(threading.Thread):
song = '唱歌。。。'
dances = '跳舞。。。'
def __init__(self,s_num,d_num):
threading.Thread.__init__(self)
self.s_num = s_num
self.d_num = d_num
def sing(self):
for i in range(self.s_num):
print(self.song)
def dance(self):
for i in range(self.d_num):
print(self.dances)
def run(self):
self.sing()
self.dance()
if __name__ == '__main__':
t = MyThread(2,1)
t.start() 本帖最后由 Cool_Breeze 于 2021-3-11 10:20 编辑
当前就只有一个线程 MyThread
执行完 run里面的内容, 这个线程就结束了 sing dance不是多线程执行的!
本帖最后由 Cool_Breeze 于 2021-3-11 10:30 编辑
import threading
import time
class SingOrDance(threading.Thread):
def __init__(self, text, count):
super().__init__()
self.__text = text
self.__count = count
def run(self):
for n in range(self.__count):
print(self.__text)
time.sleep(1)
class MyThread:
song = '唱歌。。。'
dances = '跳舞。。。'
def __init__(self,s_num,d_num):
self.s_num = s_num
self.d_num = d_num
def sing(self):
s = SingOrDance(MyThread.song, self.s_num)
s.start()
def dance(self):
d = SingOrDance(MyThread.dances, self.d_num)
d.start()
def run(self):
self.sing()
self.dance()
if __name__ == '__main__':
t = MyThread(3,2)
t.run() Cool_Breeze 发表于 2021-3-11 10:28
谢谢
页:
[1]