【Android笔记】Thread类中关于join()方法的源码分析
1、join()方法的作用:
例如有一个线程对象为Thread1,在main()方法中调用Thread1.join()方法可使得当前线程(即主线程)阻塞,而执行Thread1线程。
2、源码分析(以上面的例子为例)
- /**
- * Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
*
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis (等待时间为毫秒)
* the time to wait in milliseconds
*
* @throws IllegalArgumentException (当 millis 为负数时抛出此异常)
- * if the value of {@code millis} is negative
*
* @throws InterruptedException (如果有任何线程中断了当前线程(当前线程为main线程),抛出此异常,当前线程的中断状态将被清除。该异常由源码中的wait()抛出)
- * if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
- //这段代码是由main线程执行的
public final void join(long millis) throws InterruptedException {- synchronized(lock) { //这里的lock是在Thread1中创建的,源码为 private final Object lock=new Object();
//main线程获取对象lock的锁- long base = System.currentTimeMillis();//获取系统当前时间毫秒值
- long now = 0; //表示等待了多少时间
- if (millis < 0) { //抛出 millis 为负数的异常
- throw new IllegalArgumentException("timeout value is negative");
- }
- if (millis == 0) { //毫秒值为零,意味着等待到Thread1结束
- while (isAlive()) { //当Thread1线程还活着时
- lock.wait(0); //使得拥有lock对象的锁的线程(main线程)停止,并使得当前线程释放锁,那什么时候调用notifyAll呢?这个答案在 jvm源码中,在此不细述
- } //中断异常也是由wait()方法抛出
- } else { //否则,等待millis
- while (isAlive()) {
- long delay = millis - now;
- if (delay <= 0) {
- break; // 等待时间结束,跳出了join()方法,main线程继续执行
- }
- lock.wait(delay); //延时等待
- now = System.currentTimeMillis() - base; //更新now
- }
- }
- }
- }
【Android笔记】Thread类中关于join()方法的源码分析的更多相关文章
- Thread类中的join方法
package charpter06; //类实现接口public class Processor implements Runnable { // 重写接口方法 @Override public v ...
- Java线程状态及Thread类中的主要方法
要想实现多线程,就必须在主线程中创建新的线程对象. 不论什么线程一般具有5种状态,即创建,就绪,执行,堵塞,终止. 创建状态: 在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时 ...
- OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...
- RocketMQ中Broker的HA策略源码分析
Broker的HA策略分为两部分①同步元数据②同步消息数据 同步元数据 在Slave启动时,会启动一个定时任务用来从master同步元数据 if (role == BrokerRole.SLAVE) ...
- RxJava2 中多种取消订阅 dispose 的方法梳理( 源码分析 )
Github 相关代码: Github地址 一直感觉 RxJava2 的取消订阅有点混乱, 这样也能取消, 那样也能取消, 没能系统起来的感觉就像掉进了盘丝洞, 迷乱… 下面说说这几种情况 几种取消的 ...
- 别翻了,这篇文章绝对让你深刻理解java类的加载以及ClassLoader源码分析【JVM篇二】
目录 1.什么是类的加载(类初始化) 2.类的生命周期 3.接口的加载过程 4.解开开篇的面试题 5.理解首次主动使用 6.类加载器 7.关于命名空间 8.JVM类加载机制 9.双亲委派模型 10.C ...
- Fabric2.2中的Raft共识模块源码分析
引言 Hyperledger Fabric是当前比较流行的一种联盟链系统,它隶属于Linux基金会在2015年创建的超级账本项目且是这个项目最重要的一个子项目.目前,与Hyperledger的另外几个 ...
- Android事件分发机制浅谈(三)--源码分析(View篇)
写事件分发源码分析的时候很纠结,网上的许多博文都是先分析的View,后分析ViewGroup.因为我一开始理解的时候是按我的流程图往下走的,感觉方向很对,单是具体分析的时候总是磕磕绊绊的,老要跳到Vi ...
- jQuery原型方法.pushStack源码分析
这次分析的方法跟前面不同,虽然pushStack也是原型方法之一,但是我们几乎从不用在页面调用,在参考手册里面也没有这个方法的使用说明,但是这个方法还是非常重要的,在使用很多jQuery的其他方式都会 ...
随机推荐
- Codeforces 1355 C. Count Triangles
传送门:C - Count Triangles 题意:给你四个数A,B,C,D,求有多少个三边为x,y,z (A ≤ x ≤ B ≤ y ≤ C ≤ z ≤ D)的三角形. 题解:枚举 x=A~B, ...
- 南阳ccpc C题 The Battle of Chibi 树状数组+dp
题目: Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried ab ...
- poj2923 Relocation
Description Emma and Eric are moving to their new house they bought after returning from their honey ...
- CQRS+Event Sourcing
using System; using System.Collections.Generic; using System.Linq; namespace CQRS { public class Eve ...
- WPF 之路由事件和附加事件(六)
一.消息驱动与直接事件模型 事件的前身是消息(Message).Windows 是消息驱动的系统,运行其上的程序也遵循这个原则.消息的本质就是一条数据,这条消息里面包含着消息的类别,必要的时候还记 ...
- dict与set -- Python
dict(字典):用空间换取时间,占据空间大,但查询速度快,键值对(key:value),key唯一 d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} 由于一个k ...
- windows7 更新失败,无法开机处理方法
记录一次今天同事笔记本无法开机的故障处理 windows7系统,安装更新失败,无法开机,卡在配置更新界面 处理方法,强制关机,开机按F8,进入安全模式,安全模式还会卡在配置更新界面,但是卡一会会进入系 ...
- CSS 弹性盒子模型
CSS 弹性盒子模型 https://www.w3.org/TR/2016/CR-css-flexbox-1-20160526/ CSS Flexible Box Layout Module Leve ...
- How to build a sortable table in native js?
How to build a sortable table in native/vanilla js? H5 DnD https://developer.mozilla.org/zh-CN/docs/ ...
- .dmg & .pkg & .ipa
.dmg & .pkg & .ipa Apple, macOS, iOS .dmg mysql-8.0.21-macos10.15-x86_64.dmg https://dev.mys ...