介绍

Thread实例对象的方法
# isAlive(): 返回线程是否活动的。
# getName(): 返回线程名。
# setName(): 设置线程名。 threading模块提供的一些方法:
# threading.currentThread(): 返回当前的线程变量。
# threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
# threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
current_thread 获取当前线程对象
getName() 获取当前线程名字
from threading import Thread
from threading import current_thread
import 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 running
Thread-1 is done
'''

 

默认名字是Thread-1

getName() 获取当前线程名字,t就是current_thread() 当前线程的对象
from threading import Thread, current_thread
import 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 running
Thread-1
Thread-1 is done
'''

主线程名字默认是MainThread

from threading import Thread, current_thread
import 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
主线程 MainThread
Thread-1 is done
'''
改子线程名字 
setName()
from threading import Thread, current_thread
import 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_thread
import 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_thread
import 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 running
True
主线程
子线程1 is done
'''

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

from threading import Thread, current_thread
import 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 running
True
子线程1 is done
主线程
False
'''
activeCount(): 返回正在运行的线程数量
from threading import Thread, current_thread, active_count
import 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, enumerate
import 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)>, <Thread(子线程1, started 39424)>]
子线程1 is done
'''

python 并发编程 多线程 Thread对象的其他属性或方法的更多相关文章

  1. python 并发编程 多进程 Process对象的其他属性方法 join 方法

    一 Process对象的join方法 在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情况 情况一: 在主进程的任务与子进程的任务彼此独立的情况下 ...

  2. python 并发编程 多进程 Process对象的其他属性方法 terminate与is_alive name pid 函数

    进程对象的其他方法一: terminate与is_alive is_alive()  立刻查看的子进程结果 是否存活 from multiprocessing import Process impor ...

  3. python 并发编程 多线程 目录

    线程理论 python 并发编程 多线程 开启线程的两种方式 python 并发编程 多线程与多进程的区别 python 并发编程 多线程 Thread对象的其他属性或方法 python 并发编程 多 ...

  4. 8 并发编程-(线程)-多线程与多进程的区别&Thread对象的其他属性或方法

    1.开启速度  在主进程下开启线程比 开启子进程快 # 1 在 主进程下开启线程 from threading import Thread def work(): print('hello') if ...

  5. 并发编程 - 线程 - 1.开启线程的两种方式/2.进程与线程的区别/3.Thread对象的其他属性或方法/4.守护线程

    1.开启线程的两种方式: 进程,线程: 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合)而线程才是cpu上的执行单位) 1.同一个进程内的多个线程共享该进程内的地址资源 2.创建线 ...

  6. python并发编程&多线程(二)

    前导理论知识见:python并发编程&多线程(一) 一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 官网链 ...

  7. python并发编程&多线程(一)

    本篇理论居多,实际操作见:  python并发编程&多线程(二) 一 什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一 ...

  8. 多线程《四》Thread对象的其他属性和方法

    Thread对象的其他属性或方法 介绍 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. th ...

  9. Python并发编程——多线程与协程

    Pythpn并发编程--多线程与协程 目录 Pythpn并发编程--多线程与协程 1. 进程与线程 1.1 概念上 1.2 多进程与多线程--同时执行多个任务 2. 并发和并行 3. Python多线 ...

随机推荐

  1. Confluence 6 文件

    通过将你的文件上传到 Confluence 能够让你在一个统一的地方分享你项目小组的 PDF 文件,Office 文档,图片以及更多的内容. 自动版本,即时预览,权限控制和全文搜索意味着在网络驱动器上 ...

  2. [POI2011]DYN-Dynamite

    题目链接:Click here Solution: 直接做似乎不太可行,我们考虑二分 我们设\(f[x]\)表示以\(x\)为根的子树中选择了的节点到\(x\)的距离的最小值,初值为\(inf\) \ ...

  3. HGOI 20190711 题解

    Problem A 矩阵第K小数 给定一个$n \times m$的矩阵,位置$A_{i,j}  = i\times j$, 给出$Q$个询问,每一次查询矩阵中第$Q_i$小的数是多少. 对于100% ...

  4. Springboot入门实战, 使用@Value

    今天开始最简单的Springboot应用 entity.Book package com.draymonder.amor.entity; import java.util.List; import o ...

  5. android 小音频频繁播放

    android中多媒体文件(音乐和视频)的播放是用MediaPlayer方式是大家比较熟悉的,但是现在要来说一下另外一种音乐文件播放的方式SoundPool,相比较而言,用MediaPlayer来播放 ...

  6. fiddler(三)、会话框添加显示请求方法栏

    在使用fiddler抓包的时候,查看请求类型get和post每次只有点开该请求,在Inspectors才能查看get和post请求,不太方便.如果在会话框能直观地查看到请求方式,那就好了! 一.添加会 ...

  7. 前端性能优化 —— reflow(回流/重排)和repaint(重绘)

    简要:整个在浏览器的渲染过程中(页面初始化,用户行为改变界面样式,动画改变界面样式等)reflow(回流)和repaint(重绘) 会大大影响web性能,尤其是手机页面.因此我们在页面设计的时候要尽量 ...

  8. 上一个树形菜单的改进,增添了数据绑定功能而非仅仅的jq特效

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  9. 获取select的option值

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. 第十一周Java学习总结。

    java UI 图形界面知识梳理: ATM: 在整个AWT包中提供的所有工具类主要分为以下3种. (1)组件:Component. (2)容器:Container. (3)布局管理器:LayoutMa ...