Thread的其他属性方法

from threading import Thread,currentThread,active_count
import time

def task():
    print('%s is running' % currentThread().getName())
    time.sleep(2)
    print('%s is end'%currentThread().getName())

if __name__ == '__main__':
    t = Thread(target=task,name = '子线程1')  #可以用name定义线程名字
    t.start()
    t.join()
    t.setName('儿子线程1')   #可以更改线程名字
    currentThread().setName('zhu')  #更改主线程名字
    print(t.isAlive())
    print('zhu',currentThread().getName())

    print(active_count())   #活跃线程数量

猜你喜欢

转载自www.cnblogs.com/yelublue/p/9122545.html