介绍

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. 绑定与非绑定方法及反射,isinstance和issubclass内置函数

    目录 绑定方法与非绑定方法 1.绑定方法 2.非绑定方法(staticmethod) isinstance和issubclass 内置函数 1.isinstance 2.issubclass 反射(面 ...

  2. PLC与PC通讯

    using System; using System.Windows.Forms; using Microsoft.Win32; // for the registry table using Sys ...

  3. Python 面向对象Ⅳ

    类的继承 面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制. 通过继承创建的新类称为子类或派生类,被继承的类称为基类.父类或超类. 继承语法 在python中继承中的 ...

  4. Spring 动态多数据源

    spring springmvc mybatis 多数据源配置时的重点: 1. 注意事务拦截器的配置 Spring中的事务管理与数据源是绑定的,一旦程序执行到Service层(事务管理)的话,由于在进 ...

  5. Spring后台,通过name取值

    表单中,有同名控件(text/hidden/checkbox.......)的情况下,采用getParameterValues("name"):String[] 表单中,只有一个n ...

  6. CF873F Forbidden Indices 后缀自动机+水题

    刷刷水~ Code: #include <cstdio> #include <cstring> #include <algorithm> #define N 200 ...

  7. Nowcoder 练习赛26 D xor序列 ( 线性基 )

    题目链接 题意 : 中文题.点链接 分析 : 对于给定的 X 和 Y 假设存在一个 Z 使得 X (xor) Z = Y 做一个变形 X (xor) Z (xor) Y = 0 X (xor) Y = ...

  8. poj 2976 Dropping tests 二分搜索+精度处理

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8349   Accepted: 2919 De ...

  9. JavaWeb_初识过滤器Filter

    菜鸟教程 传送门 过滤器Filter::JavaWeb三大组件之一,它与Servlet很相似,过滤器是用来拦截请求的,而不是处理请求的 当用户请求某个Servlet时,会先执行部署在这个请求上的Fil ...

  10. is == 编码与解码

    is 和 ==  主要是数字和字符串的比较 1 区别: ==比较的是两边的值     is比较的是两边值的id    id获取的方法 id() 2 小数据池: -5~256 3 字符串中特殊字符有id ...