今天小王请xiaoming和xiaowang吃火锅,吃完火锅的时候会有如下三种场景:python
场景一:小王(主)先吃完了,海海(客)和老王(客)还没吃完,这种场景会致使结帐的人先走了,剩下两个小伙伴傻眼了。。。函数
场景二:小王(主)先吃完了,海海和老王还没吃饱,一块儿结帐走人。spa
场景三:小王(主)先等海海和老王吃饱了,小编最后结帐一块儿走人。线程
场景一:主线程已经结束了,子线程还在跑3d
1.咱们把thread1.start()和thread2.start()称为两个子线程,写在外面的代码就是主线程了。code
# -*- coding:utf-8 -*-
import time
import threading
def chiHuoGuo(people):
print("%s 吃火锅的小伙伴-羊肉:%s" % (time.ctime(),people))
time.sleep(1)
print("%s 吃火锅的小伙伴-鱼丸:%s" % (time.ctime(),people))
class myThread(threading.Thread):
def __init__(self, people, name):
'''重写threading.Thread初始化内容'''
threading.Thread.__init__(self)
self.threadNmae = name
self.people = people
def run(self):
print("开始线程:" + self.threadNmae)
chiHuoGuo(self.people)
print("结束线程:" + self.threadNmae)
if __name__ == "__main__":
# 启动线程
thread1 = myThread("xiaoming", "Thread-1")
thread2 = myThread("xiaowang", "Thread-2")
thread1.start()
thread2.start()
time.sleep(0.1)
print("退出主线程:吃火锅,结帐走人")
场景二:主线程结束了,子线程必须也跟着结束orm
1.主线程中,建立了子线程 线程A和线程B,而且在主线程中调用了thread.setDaemon(),这个的意思是,把主线程设置为守护线程,这时候,要是主线程执行结束了,就无论子线程是否完成,一并和主线程退出.
(敲黑板:必须在start()方法调用以前设置,若是不设置为守护线程,程序会被无限挂起。)blog
2.线程有一个布尔属性叫作daemon。表示线程是不是守护线程,默认取否。当程序中的线程所有是守护线程时,程序才会退出。只要还存在一个非守护线程,程序就不会退出。
主线程是非守护线程。继承
3.setDaemon(True)此方法里面参数设置为True才会生效utf-8
# coding=utf-8
import threading
import time
def chiHuoGuo(people):
print("%s 吃火锅的小伙伴-羊肉:%s" % (time.ctime(),people))
time.sleep(1)
print("%s 吃火锅的小伙伴-鱼丸:%s" % (time.ctime(),people))
class myThread (threading.Thread): # 继承父类threading.Thread
def __init__(self, people, name):
'''重写threading.Thread初始化内容'''
threading.Thread.__init__(self)
self.threadName = name
self.people = people
def run(self): # 把要执行的代码写到run函数里面 线程在建立后会直接运行run函数
'''重写run方法'''
print("开始线程: " + self.threadName)
chiHuoGuo(self.people) # 执行任务
print("结束线程: " + self.name)
print("yoyo请小伙伴开始吃火锅:!!!")
# 建立新线程
thread1 = myThread("xiaoming", "Thread-1")
thread2 = myThread("xiaowang", "Thread-2")
# 守护线程setDaemon(True)
thread1.setDaemon(True) # 必须在start以前
thread2.setDaemon(True)
# 开启线程
thread1.start()
thread2.start()
time.sleep(0.1)
print("退出主线程:吃火锅结束,结帐走人")
4.运行结果:
1.若是想让主线程等待子线程结束后再运行的话,就须要用到join(),此方法是在start以后(与setDaemon相反)
2.join(timeout)此方法有个timeout参数,是线程超时时间设置。
# coding=utf-8 import threading import time def chiHuoGuo(people): print("%s 吃火锅的小伙伴-羊肉:%s" % (time.ctime(),people)) time.sleep(1) print("%s 吃火锅的小伙伴-鱼丸:%s" % (time.ctime(),people)) class myThread (threading.Thread): # 继承父类threading.Thread def __init__(self, people, name): '''重写threading.Thread初始化内容''' threading.Thread.__init__(self) self.threadName = name self.people = people def run(self): # 把要执行的代码写到run函数里面 线程在建立后会直接运行run函数 '''重写run方法''' print("开始线程: " + self.threadName) chiHuoGuo(self.people) # 执行任务 print("结束线程: " + self.name) print("yoyo请小伙伴开始吃火锅:!!!") # 建立新线程 thread1 = myThread("xiaoming", "Thread-1") thread2 = myThread("xiaowang", "Thread-2") # 开启线程 thread1.start() thread2.start() # 阻塞主线程,等子线程结束 thread1.join() thread2.join() time.sleep(0.1) print("退出主线程:吃火锅结束,结帐走人")
运行结果: