线程中的join方法
join方法的作用是同步线程。
1、不使用join方法:当设置多个线程时,在一般情况下(无守护线程,setDeamon=False),多个线程同时启动,主线程执行完,会等待其他子线程执行完,程序才会退出。
def print_number(num):
print("-----> %d" % num, time.ctime())
time.sleep(5)
print("print_number ending......", time.ctime())
def print_letter(letter):
print("-----> %s" % letter, time.ctime())
time.sleep(10)
print("print_letter ending......", time.ctime())
t1 = threading.Thread(target=print_number,args=(10,))
t2 = threading.Thread(target=print_letter,args=('hello',))
if __name__ == '__main__':
t1.start()
t2.start()
print("Main thread ended...")
显示结果如下:
-----> 10 Wed Mar 20 09:22:30 2019
-----> hello Wed Mar 20 09:22:30 2019
Main thread ended...
print_number ending...... Wed Mar 20 09:22:35 2019
print_letter ending...... Wed Mar 20 09:22:40 2019 ***Repl Closed***
可以看到主线程、t1、t2同时开始执行,主线程仅打印了“Main thread ended...”,就执行完毕并退出;而程序等待t1和t2执行完毕后才退出。
2、使用join()方法:主线程主线程任务结束之后,进入阻塞状态,一直等待调用join方法的子线程执行结束之后,主线程才会终止。下面的例子是让t1调用join()方法。
import threading
import time def print_number(num): print("-----> %d" % num, time.ctime())
time.sleep(5)
print("print_number ending......", time.ctime()) def print_letter(letter): print("-----> %s" % letter, time.ctime())
time.sleep(10)
print("print_letter ending......", time.ctime()) t1 = threading.Thread(target=print_number,args=(10,))
t2 = threading.Thread(target=print_letter,args=('hello',)) if __name__ == '__main__':
t1.start()
t2.start() t1.join() print("Main thread ended...")
程序运行结果如下:
-----> 10 Wed Mar 20 09:20:59 2019
-----> hello Wed Mar 20 09:20:59 2019
print_number ending...... Wed Mar 20 09:21:04 2019
Main thread ended...
print_letter ending...... Wed Mar 20 09:21:09 2019 ***Repl Closed***
可以看到与无join方法时的输出顺序明显不同。t1、t2同时启动。而t1的时间短,因此在5秒钟之后该线程执行完毕。由于t1.join(),主线程在执行时,在调用t1.join()方法的地方被阻塞了,在此处一直等待t1执行结束,主线程才会执行最后一行代码。
线程中的join方法的更多相关文章
- 线程中的join方法,与synchronized和wait()和notify()的关系
什么时候要用join()方法? 1,join方法是Thread类中的方法,主线程执行完start()方法,线程就进入就绪状态,虚拟机最终会执行run方法进入运行状态.此时.主线程跳出start方法往下 ...
- 模拟做饭系统(java+线程中的join方法)
(一)项目框架分析 妈妈要去做饭,发现没有酱油,让儿子去买酱油,然后回来做饭. 根据面向对象的思想,有两个对象,妈妈和儿子 主要有两个方法: (一)没有线程控制(即儿子没有买酱油回来妈妈就做好饭了)+ ...
- Java多线程中的join()方法
一.join()方法介绍 join() 定义在Thread.java中.join()方法把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的join( ...
- Java并发编程--多线程中的join方法详解
Java Thread中, join()方法主要是让调用该方法的thread在完成run方法里面的部分后, 再执行join()方法后面的代码 例如:定义一个People类,run方法是输出姓名年龄. ...
- Thread类中的join方法
package charpter06; //类实现接口public class Processor implements Runnable { // 重写接口方法 @Override public v ...
- 线程中的join()
http://blog.itpub.net/31555134/viewspace-2221319/ 一直对join()方法不是很理解,在A线程中, B线程调用了join()方法,然后在内部实际是wai ...
- 3-等待线程终止的join方法
等待线程终止的join方法 在项目实践中经常会遇到一个场景,就是需要等待某几件事完成之后才能继续往下执行,比如线程加载资源等等. package com.heiye.learn1; public cl ...
- jQuery中的join方法
和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...
- java线程学习之join方法
join()方法表示一个线程要加入另一个线程,直到被加入的线程执行完毕. 这个概念不好理解的话看面这个例子 public class TestJoin { public static void mai ...
随机推荐
- Redis阻塞诊断基础
slowlog Redis慢查询 slowlog 参数 slowlog-log-slower-than: 慢查询时间阈值,超过这个阈值的查询将会被记录,默认值10000,但是微妙,也即10毫秒. sl ...
- 最大化系统并发连接数.Windows.reg
最大化系统并发连接数.Windows.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentContro ...
- 2018.5.12 storm数据源kafka堆积
问题现象: storm代码依赖4个源数据topic,2018.5.12上午8点左右开始收到告警短信,源头的4个topic数据严重堆积. 排查: 1.查看stormUI, storm拓扑结构如下: 看现 ...
- 一个box四周边框阴影
实现效果如图: 代码如下: .section { margin: 20upx 30upx; padding: 40upx; border-radius: 6px; border-top: #0670C ...
- PTA变成总结1
1)实验代码 #include<stdio.h>int main(){ int N,k,n,i,j,p,m,ge; char op; while(1){ scanf("%d % ...
- cs架构与bs架构的对比
主要区别: Client/Server是建立在局域网的基础上的.Browser/Server是建立在广域网的基础上的. 1.硬件环境不同 C/S 一般建立在专用的网络上, 小范围里的网络环境, 局域网 ...
- java后台动态生成导出excel
p ublic void export(List<WechatUser> wechatUserList, HttpServletResponse response) throws IOEx ...
- AttributeError: 'LoginForm' object has no attribute 'is_bound' , object has no attribute 'is_bound'
'LoginForm' object has no attribute 'is_bound' 可能原因: 啥子问题?? 都是 jquery.js 文件.....
- pom.xml文件释义
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 51单片机学习笔记(清翔版)(21)——ADDA数模转换
A:anolog模拟的 D:digital数字的 AD模拟转数字,DA数字转模拟 生活中的基本都是模拟量,如温度,可以是10℃,10.1℃等 手机的背光亮度自动调节,拿到太阳光下,亮度 ...