mike = threading.Thread(target=thread_run, args=(‘Mike’, ))
jone = threading.Thread(target=thread_run, args=(‘jone’, ))
mike.start()
jone.start()
#mike.join() #阻塞子线程mike
#jone.join() #阻塞子线程jone
print(‘main thread is running!!!’)
执行结果:
main thread is running!!!
jone’s first thread!!!
Mike’s first thread!!!
3. isAlive = is_alive(self)
help解释:Return whether the thread is alive.
这个方法用于判断线程是否运行。
-
当线程未调用 start()来开启时,isAlive()会返回False
-
但线程已经执行后并结束时,isAlive()也会返回False
‘’’
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
‘’’
import time
import threading
def thread_run(name):
time.sleep(2)
print(“%s’s first thread!!!”% name)
mike = threading.Thread(target=thread_run, args=(‘Mike’, ))
jone = threading.Thread(target=thread_run, args=(‘jone’, ))
mike.start()
jone.start()
print(‘isAlive status: %s’% mike.isAlive())
print(‘is_alive status: %s’ %mike.is_alive())
print(‘main thread is running!!!’)
执行结果:
isAlive status: True
is_alive status: True
main thread is running!!!
Mike’s first thread!!!
jone’s first thread!!!
4. name
help解释:A string used for identification purposes only.
name属性表示线程的线程名 默认是 Thread-x x是序号,由1开始,第一个创建的线程名字就是 Thread-1
import time
import threading
def thread_run(name):
print(“%s’s first thread!!!”% name)
time.sleep(5)
mike = threading.Thread(target=thread_run, args=(‘Mike’, ), name=‘Thread-mike’) #name设置线程名
jone = threading.Thread(target=thread_run, args=(‘jone’, )) #默认线程name是Thread-X
mike.start()
jone.start()
print(mike.name) #打印线程名
print(jone.name) #打印线程名
执行结果:
Mike’s first thread!!!
jone’s first thread!!!
Thread-mike
Thread-1
5. setName()
用于设置线程的名称name
‘’’
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
‘’’
import time
import threading
def thread_run(name):
print(“%s’s first thread!!!”% name)
time.sleep(5)
mike = threading.Thread(target=thread_run, args=(‘Mike’, ))
jone = threading.Thread(target=thread_run, args=(‘jone’, )) #默认线程name是Thread-X
mike.setName(‘Thread-mike’) #name设置线程名
mike.start()
jone.start()
print(mike.name) #打印线程名
print(jone.name) #打印线程名
执行结果:
Mike’s first thread!!!
jone’s first thread!!!
Thread-mike
Thread-2
6. getName()
获取线程名称name
‘’’
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
‘’’
import time
import threading
def thread_run(name):
print(“%s’s first thread!!!”% name)
time.sleep(5)
mike = threading.Thread(target=thread_run, args=(‘Mike’, ))
jone = threading.Thread(target=thread_run, args=(‘jone’, )) #默认线程name是Thread-X
mike.setName(‘Thread-mike’) #name设置线程名
mike.start()
jone.start()
print(mike.getName()) #打印线程名
print(jone.getName()) #打印线程名
7. daemon
help解释:A boolean value indicating whether this thread is a daemon thread
当 daemon = False 时,线程不会随主线程退出而退出(默认时,就是 daemon = False)
当 daemon = True 时,当主线程结束,其他子线程就会被强制结束
import time
import threading
def thread_mike_run(name):
time.sleep(1)
print(‘mike thread is running 1S’)
time.sleep(5)
print(“%s’s first thread!!!”% name)
def thread_jone_run(name):
time.sleep(2)
print(“%s’s first thread!!!”% name)
mike = threading.Thread(target=thread_mike_run, args=(‘Mike’, ), daemon=True) #设置daemon为True
#mike = threading.Thread(target=thread_mike_run, args=(‘Mike’, ))
jone = threading.Thread(target=thread_jone_run, args=(‘jone’, ))
mike.start()
jone.start()
print(‘main thread’) #由于线程没有join(),所以主线程会先运行;而jone的daemon为false,所以主线程会等待jone线程运行完毕;但是mike的daemon为True,所以主线程不会等待mike线程
执行结果:
main thread
mike thread is running 1S #mike线程只执行了1S的print,5S的为执行就直接退出了
jone’s first thread!!!
8. setDaemon()
用于设置daemon值
import time
import threading
def thread_mike_run(name):
time.sleep(1)
print(‘mike thread is running 1S’)
time.sleep(5)
print(“%s’s first thread!!!”% name)
def thread_jone_run(name):
time.sleep(2)
print(“%s’s first thread!!!”% name)
mike = threading.Thread(target=thread_mike_run, args=(‘Mike’, ))
jone = threading.Thread(target=thread_jone_run, args=(‘jone’, ))
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)
C-1713682293308)]
[外链图片转存中…(img-5eA6nMJQ-1713682293309)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)