博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 并发编程 多线程 Thread对象的其他属性或方法
阅读量:4649 次
发布时间:2019-06-09

本文共 4432 字,大约阅读时间需要 14 分钟。

 

 

介绍

Thread实例对象的方法  # isAlive(): 返回线程是否活动的。  # getName(): 返回线程名。  # setName(): 设置线程名。threading模块提供的一些方法:  # threading.currentThread(): 返回当前的线程变量。  # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。  # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

 

 

current_thread 获取当前线程对象 getName() 获取当前线程名字
from threading import Threadfrom threading import current_threadimport time# current_thread 获取当前线程对象名字# getName() 获取当前线程名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task,)    t.start()'''Thread-1 is runningThread-1 is done'''
 
 

 默认名字是Thread-1

getName() 获取当前线程名字,t就是current_thread() 当前线程的对象
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task,)    t.start()    print(t.getName())  # = current_thread().getName()'''Thread-1 is runningThread-1Thread-1 is done'''

 

 主线程名字默认是MainThread

from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task,)    t.start()    print("主线程", current_thread().getName()) # 打印主线程名字'''Thread-1 is running主线程 MainThreadThread-1 is done'''

 

 

改子线程名字 setName()
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    # name 改子线程名字    t = Thread(target=task, name="子线程1")    t.start()    # 改子线程名字    t.setName("儿子线程1")    print("主线程", current_thread().getName())  # 打印主线程名字'''子线程1 is running主线程 MainThread儿子线程1 is done'''

启动程序瞬间开启子线程 

 

 

改主线程名字

current_thread.setName()

from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task, name="子线程1")    t.start()    current_thread().setName("主线程helo")    print("主线程", current_thread().getName())'''子线程1 is running主线程 主线程helo子线程1 is done'''

 

t.isAlive() 查看子进程是否存活
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task, name="子线程1")    t.start()    # 判断子线程是否存活    print(t.isAlive())    print("主线程")'''子线程1 is runningTrue主线程子线程1 is done'''

 

主线程等待子线程运行完,主线程再执行 join()

from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task, name="子线程1")    t.start()    print(t.isAlive())    t.join()    # 判断子线程是否存活    print("主线程")    print(t.isAlive())'''子线程1 is runningTrue子线程1 is done主线程False'''

 

activeCount(): 返回正在运行的线程数量
from threading import Thread, current_thread, active_countimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task, name="子线程1")    t.start()    t.join()    print("主线程")    # 返回正在运行的线程数量    print(active_count())'''子线程1 is running子线程1 is done主线程1只剩下主线程'''

 

enumerate() 返回一个包含正在运行的线程的列表list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
from threading import Thread, current_thread, enumerateimport time# current_thread 获取当前线程对象名字def task():    print("%s is running" % current_thread().getName())    time.sleep(2)    print("%s is done" % current_thread().getName())if __name__ == '__main__':    t = Thread(target=task, name="子线程1")    t.start()    print("主线程")    # 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。    print(enumerate())'''子线程1 is running主线程[<_MainThread(MainThread, started 38668)>, 
]子线程1 is done'''

 

转载于:https://www.cnblogs.com/mingerlcm/p/9018917.html

你可能感兴趣的文章
用javascript将数据导入Excel
查看>>
novoton-timer使用
查看>>
[Office]PPT 2013如何设置图片为半透明?
查看>>
原生js实现浏览器全屏和退出全屏
查看>>
选择排序(c++)
查看>>
特殊文件(下)
查看>>
ubuntu通过vmware与访问宿主的文件
查看>>
mysql 5.7 二进制安装方法
查看>>
244. Shortest Word Distance II
查看>>
385. Mini Parser
查看>>
React-组件的生命周期
查看>>
Git详解之四:服务器上的Git
查看>>
JavaScript 复杂判断的更优雅写法借鉴
查看>>
<mvc:annotation-driven/>浅析
查看>>
ArcEngine开发之自定义工具
查看>>
SQL视频总结
查看>>
P4878 道路修建-美国
查看>>
dp练习
查看>>
vim
查看>>
maze_travel的隐私声明
查看>>