def run(self):
print("%s's first thread!!!"% self.name)
time.sleep(5)
mike = mythread('mike')
jone = mythread('jone')
mike.start()
jone.start()
线程threading.Thread实例常用方法
可以通过help(threading.Thread)查看线程实例所有方法
1.start()
help解释:Start the thread’s activity
启动线程
2.join()
help解释:Wait until the thread terminates
阻塞线程直至线程终止,然后在继续运行
'''
遇到问题没人解答?小编创建了一个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()
mike.join() #阻塞子线程mike直到mike线程执行完毕
jone.join() #阻塞子线程jone直到jone线程执行完毕
print('main thread is running!!!')
执行结果:
jone's first thread!!!
Mike's first thread!!!
main thread is running!!!
如果上面列子不执行join(),主线程先执行,然后才会执行子线程mike和jone
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()
#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()