Thread对象的其他属性或方法

Thread对象的其他属性或方法

介绍

Thread实例对象的方法
  # isAlive(): 返回线程是否活动的。
  # getName(): 返回线程名。
  # setName(): 设置线程名。

threading模块提供的一些方法:
  # threading.currentThread(): 返回当前的线程变量。
  # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
  # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

getName()和setName()

from threading import Thread,currentThread
import time
# 当前线程的对象 currentThread

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

if __name__ == "__main__":
    t = Thread(target=task, name="线程1")  # t=currentThread()
    t.start()
    t.setName("儿子线程1")   # 改线程名字

    # print(t.getName())
    currentThread().setName("主线程名称")   # 该主线程名字
    print("<主线程>", currentThread().getName())

  执行结果

线程1 is running
主线程 主线程名称
儿子线程1 is done

isAlive()

from threading import Thread, currentThread
import time
# 当前线程的对象 currentThread

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

if __name__ == "__main__":
    t = Thread(target=task, name="线程1")  # t=currentThread()
    t.start()

    print(t.isAlive())     # 查看是否存

  执行结果

线程1 is running
True
线程1 is done

 

主线程等待子线程结束

from threading import Thread, currentThread
import time
# 当前线程的对象 currentThread

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

if __name__ == "__main__":
    t = Thread(target=task, name="线程1")  # t=currentThread()
    t.start()
    t.join()

    print(t.isAlive())     # 查看是否存活

执行结果

线程1 is running
线程1 is done
False

active_count()

from threading import Thread, currentThread, active_count
import time
# 当前线程的对象 currentThread

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

if __name__ == "__main__":
    t = Thread(target=task, name="线程1")  # t=currentThread()
    t.start()

    print(active_count())  # 活跃进程数

 执行结果:

线程1 is running
2
线程1 is done

只剩下主线程

from threading import Thread, currentThread, active_count
import time
# 当前线程的对象 currentThread

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

if __name__ == "__main__":
    t = Thread(target=task, name="线程1")  # t=currentThread()
    t.start()
    t.join()

    print(active_count())  # 活跃进程数

执行结果:

线程1 is running
线程1 is done
1

enumerate()

from threading import Thread, currentThread, active_count, enumerate
import time
# 当前线程的对象 currentThread

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

if __name__ == "__main__":
    t = Thread(target=task, name="线程1")  # t=currentThread()
    t.start()
    # t.join()

    print(enumerate())  # 把当前活跃的线程对象取出

  执行结果:

线程1 is running
[<_MainThread(MainThread, started 10552)>, <Thread(线程1, started 3740)>]
线程1 is done

posted @ 2018-12-17 01:45  混世妖精  阅读(272)  评论(0编辑  收藏  举报