Thread对象的其他属性或方法
Thread实例对象的方法 # isAlive(): 返回线程是否活动的。 # getName(): 返回线程名。 # setName(): 设置线程名。 threading模块提供的一些方法: # threading.currentThread(): 返回当前的线程变量。 # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。 # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
例子: 查看线程名字
from threading import Thread, current_thread import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,) # 其中current_thread()就是拿到t1(也就是拿到t1这对象) t1.start() print(t1.getName()) print("主线程名字",current_thread().getName())
Thread-1 is running # 子线程名字默认是Thread名字开头 Thread-1 主线程名字 MainThread # 主线程名字默认是MainThread
2. 改线程名字
from threading import Thread, current_thread import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,name="子线程") # 其中current_thread()就是拿到t1 t1.start() # t1.setName("son线程") print(t1.getName()) print("主线程名字",current_thread().getName())
子线程 is running son线程 主线程名字 MainThread son线程 is done
接下来该线程名字
from threading import Thread, current_thread import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,name="子线程") # 其中current_thread()就是拿到t1 t1.start() t1.setName("son线程") # 修改线程名字 print(t1.getName()) print("主线程名字",current_thread().getName()
子线程 is running son线程 主线程名字 MainThread son线程 is done
修改主线程名字
from threading import Thread, current_thread import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,name="子线程") # 其中current_thread()就是拿到t1 t1.start() t1.setName("son线程") print(t1.getName()) current_thread().setName("主线程") print("主线程名字",current_thread().getName())
子线程 is running son线程 主线程名字 主线程 # 名字修改成功 son线程 is done
查看存活
from threading import Thread, current_thread import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,name="子线程") # 其中current_thread()就是拿到t1 t1.start() # t1.setName("son线程") # print(t1.getName()) # current_thread().setName("主线程") print(t1.isAlive()) print("主线程名字",current_thread().getName())
子线程 is running True 主线程名字 MainThread 子线程 is done
等待
from threading import Thread, current_thread import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,name="子线程") # 其中current_thread()就是拿到t1 t1.start() t1.join() # t1.setName("son线程") # print(t1.getName()) # current_thread().setName("主线程") print(t1.isAlive()) print("主线程名字",current_thread().getName())
子线程 is running 子线程 is done False 主线程名字 MainThread
查看线程总数 activeCount
from threading import Thread, current_thread,activeCount import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,name="子线程") # 其中current_thread()就是拿到t1 t1.start() # t1.setName("son线程") # print(t1.getName()) # current_thread().setName("主线程") # print(t1.isAlive()) # print("主线程名字",current_thread().getName()) print(activeCount())
子线程 is running 2 子线程 is done
查看当前线程对象 enumerate
from threading import Thread, current_thread,activeCount,enumerate import time def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName()) if __name__ == "__main__": t1 = Thread(target=task,name="子线程") # 其中current_thread()就是拿到t1 t1.start() print(enumerate())
子线程 is running [<Thread(子线程, started 8492)>, <_MainThread(MainThread, started 6932)>] 子线程 is done