总结:
- 线程和进程都是操作系统的基本理论;当前线程(工作线程、主线程)、工作线程、主线程的区别;
2. 线程是一种并行的解决并发的方案;
3.. 线程的name这是一个名称,可以重复;ID必须唯一,但可以在线程退出后再利用。- 使用start方法启动线程,启动了一个新的线程,名字叫做worker运行。但是使用run方法的,并没有启动新的线程,就是在主线程中调用了一个普通的函数而已。 单线程不会有并行的效果(单线程类似于函数压栈)
- 操作系统提供的接口成为系统调用;操作系统调度的最小单位是线程;
- 串行和并行的问题;各有各的优缺点;不是多多益善;
- 多线程是基本功;
线程的状态:启动、运行和消亡
1. threading的属性和方法
JVM虚拟化
名称 | 含义 |
---|---|
current_thread() | 返回当前线程对象 |
main_thread() | 返回主线程对象 |
active_count() | 当前处于alive状态的线程个数 |
enumerate() | 返回所有活着的线程的列表,不包括已经终止的线程和未开始的线程 |
get_ident() | 返回当前线程ID, 非0整数; |
线程是一种并行的解决并发的方案;
import threading
import time
def showthreadinfo():
print("currentthread = {}".format(threading.current_thread()))
print("main thread = {}".format(threading.main_thread()))
print("acive count = {}".format(threading.active_count()))
def worker():
count = 0
showthreadinfo()
while True:
if (count > 3):
break
time.sleep(1)
count += 1
print("I'm working")
t = threading.Thread(target=worker, name='worker') # 线程对象
showthreadinfo()
t.start() # 启动
print('==End==')
#---------------------------------------------------------------------------------------
currentthread = <_MainThread(MainThread, started 17136)>
main thread = <_MainThread(MainThread, started 17136)>
acive count = 1
currentthread = <Thread(worker, started 13496)>==End==
==End==
main thread = <_MainThread(MainThread, stopped 17136)>
acive count = 2
I'm working
I'm working
I'm working
I'm working
当前线程(工作线程、主线程)
工作线程和主线程同时在跑(打印currentthread 后,打印end接着打印下面的信息),这就是并行的概念;
能不能不等主线程的打印,全部打印工作线程的信息呢?这就是我们后面要学习的;
2. Thread实例的属性和方法
注意:线程的name这是一个名称,可以重复;ID必须唯一,但可以在线程退出后再利用。
2.1 Thread实例
名称 | 含义 |
---|---|
name | 只是一个名字,只是个标识,名称可以重名。getName()、setName()获取、设置这个名词 |
ident | 线程ID,它是非0整数。线程启动后才会有ID,否则为None。线程退出,此ID依旧可以访问。此ID可以重复使用 |
is_alive() | 返回线程是否活着 |
线程对象仅可启动一次;重新建立线程对象,
import threading
import time
def showthreadinfo():
print("currentthread = {}".format(threading.current_thread()))
print("main thread = {}".format(threading.main_thread()))
print("acive count = {}".format(threading.active_count()))
def worker():
count = 0
#showthreadinfo()
while True:
if (count > 5):
break
time.sleep(1)
count += 1
print("I'm working")
t = threading.Thread(target=worker, name='worker') # 线程对象
print(t.ident)
t.start() # 启动
while True:
time.sleep(1)
if t.is_alive():
print('{} {} alive'.format(t.name, t.ident))
else:
print('{} {} dead'.format(t.name, t.ident))
t.start() # 可以吗? # 线程对象仅可启动一次;
#-----------------------------------------------------------------------------------
None
worker 6844 alive
I'm working
worker 6844 alive
I'm working
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/learn/36/36.3.py", line 36, in <module>
t.start() # 可以吗?
File "C:\ProgramData\Miniconda3\lib\threading.py", line 843, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
Process finished with exit code 1
2.2 Thread实例的run 和 start 方法
start 方法和 run方法有什么不同呢?
名称 | 含义 |
---|---|
start() | 启动线程。每一个线程必须且只能执行该方法一次 |
run() | 运行线程函数 |
单独看 start和 run方法;
# start方法;
import threading
import time
def showthreadinfo():
print("currentthread = {}".format(threading.current_thread()))
print("main thread = {}".format(threading.main_thread()))
print("acive count = {}".format(threading.active_count()))
class MyThread(threading.Thread):
def start(self):
print('start~~~~~~~~~~~~~')
super().start()
def run(self):
print('run~~~~~~~~~~~~~')
super().run()
def worker():
count = 0
showthreadinfo()
while True:
if (count > 5):
break
time.sleep(1)
count += 1
print("I'm working")
t = MyThread(target=worker, name='worker') # 线程对象
t.start() # 启动
print(t.ident)
#-------------------------------------------------------------------------------------
start~~~~~~~~~~~~~
run~~~~~~~~~~~~~
22972
currentthread = <MyThread(worker, started 21344)>
main thread = <_MainThread(MainThread, stopped 17160)>
acive count = 2
I'm working
# run方法;
import threading
import time
def showthreadinfo():
print("currentthread = {}".format(threading.current_thread()))
print("main thread = {}".format(threading.main_thread()))
print("acive count = {}".format(threading.active_count()))
class MyThread(threading.Thread):
def start(self):
print('start~~~~~~~~~~~~~')
super().start()
def run(self):
print('run~~~~~~~~~~~~~')
super().run()
def worker():
count = 0
#showthreadinfo()
while True:
if (count > 5):
break
time.sleep(1)
count += 1
print("I'm working")
t = MyThread(target=worker, name='worker') # 线程对象
t.run() # 启动
print(t.ident)
#------------------------------------------------
run~~~~~~~~~~~~~
currentthread = <_MainThread(MainThread, started 4608)>
main thread = <_MainThread(MainThread, started 4608)>
acive count = 1
I'm working
None
start()方法会调用run()方法,而run()方法可以运行函数。
这两个方法看似功能重复了,这么看来留一个方法就可以了。是这样吗?
使用start方法启动线程,启动了一个新的线程,名字叫做worker运行。但是使用run方法的,并没有启动新的线程,就是在主线程中调用了一个普通的函数而已。
因此,启动线程请使用start方法,才能启动多个线程。单线程不会有并行的效果(单线程类似于函数压栈);
网友评论