统计线程数量

from threading import Thread

import threading
import time



def sing():
    for i in range(3):
        print('唱第%d首歌'%(i))
        time.sleep(1)

def dance():
    for i in range(3):
        print('跳第%d段舞'%(i))
        time.sleep(1)

if __name__ == '__main__':
    t1 = Thread(target=sing)
    t2 = Thread(target=dance)

    t1.start()
    t2.start()

    #查看线程的数量
    while True:
        count = len(threading.enumerate())#当前活着的线程数
        print('当前线程数量:',count)
        if count <=1:
            break
    #注意:至少得有一个主线程

你可能感兴趣的:(统计线程数量)