线程数量问题
import time,random
from threading import Thread
import threading
def sing():
for i in range(3):
print("唱第%d首歌"%(i))
time.sleep(random.random())
def dance():
for i in range(3):
print("跳第%d段舞"%(i))
time.sleep(random.random())
if __name__ == '__main__':
t1=Thread(target=sing,)
t2=Thread(target=dance,)
t1.start()
t2.start()
while True:
count=len(threading.enumerate()) #threading.enumerate()查看线程数量
print("线程个数:",count)
if count==1:
break
# 注意:至少得有一个主线程
线程实现的第二种方式
from threading import Thread
class MyThread(Thread):
def __init__(self,url):
Thread.__init__(self)
self.url=url
def run(self):
print("线程的业务逻辑代码写在这里!")
if __name__ == '__main__':
url="http://img.netbian.com/file/2019/0311/small91c78151d9fe112e49d322fab157abd11552278397.jpg",
t=MyThread(url)
t.start()#多线程的执行顺序是不一定的