java基础知识回顾之java Thread类学习(十一)--join方法的理解
以下面例子说明下面的源码:main 线程 和 A线程,A线程是main线程创建并且启动的,main线程优先级比较高,正在执行;
这个时候main线程调用A.join()之后,main线程一直等待,直到A线程运行完毕,main线程才运行。
join方法的源码:
* 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
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
// 加锁当前线程
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0; if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
} if (millis == 0) {
//A线程是start,在运行中
while (isAlive()) {
//main线程等待
wait(0);
}
} else {
//join(timeOut)的情况
while (isAlive()) {
//根据当前timeout的时间-now 是否<=0进行判断,主线程是否继续阻塞等待
long delay = millis - now;
//等待超时时间到了,则主线程不阻塞了,等待结束
if (delay <= 0) {
break;
}
wait(delay);
//子线程从循环开始到现在运行的时间
now = System.currentTimeMillis() - base;
}
}
}
给一个例子来理解:
package concurrentMy.joins; /**
*
* @author Administrator
*
* main 线程 和 A线程,A线程是main线程创建并且启动的,main线程优先级比较高,正在执行;
* 这个时候main线程调用A.join()之后,main线程一直等待,直到A线程运行完毕,main线程才运行
*
*/
class ThreadA extends Thread { public ThreadA(String name){
super(name);
} @Override
public void run() { for (int i = 0; i < 20; i++) {
// 复写父类的toString方法, 返回该线程的字符串表示形式,包括线程名称、优先级和线程组。
System.out.println(Thread.currentThread().getName() + "-" + i); }
} } public class JoinDemo { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
ThreadA A = new ThreadA("线程A");
A.start();
A.join(); //A线程加入到“main线程”中,main线程一直等待,直到A线程执行完毕,main线程才运行
System.out.println(Thread.currentThread().getName() + "执行");
System.out.println(Thread.currentThread().getName() + "执行终止"); } }
输出结果:
线程A-0
线程A-1
线程A-2
线程A-3
线程A-4
线程A-5
线程A-6
线程A-7
线程A-8
线程A-9
线程A-10
线程A-11
线程A-12
线程A-13
线程A-14
线程A-15
线程A-16
线程A-17
线程A-18
线程A-19
main执行
main执行终止
代码运行整个过程说明入下图:
java基础知识回顾之java Thread类学习(十一)--join方法的理解的更多相关文章
- java基础知识回顾之---java String final类普通方法
辞职了,最近一段时间在找工作,把在大二的时候学习java基础知识回顾下,拿出来跟大家分享,如果有问题,欢迎大家的指正. /* * 按照面向对象的思想对字符串进行功能分类. * ...
- java基础知识回顾之java Thread类学习(三)--java线程实现常见的两种方式实现好处:
总结:实现Runnable接口比继承Thread类更有优势: 1.因为java只能单继承,实现Runnable接口可以避免单继承的局限性 2.继承Thread类,多个线程不能处理或者共享同一个资源,但 ...
- java基础知识回顾之java Thread类学习(十)--线程的状态以及转化使用的方法介绍
线程的概述: 线程是程序的多个执行路径,执行调度的单位,依托于进程存在.线程不仅可以共享进程的内存,而且还拥有一个属于自己的内存空间,这段内存空间叫做线程栈,是建立线程的时候由系 ...
- java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)
1.wait和notify,notifyAll: wait和notify,notifyAll是Object类方法,因为等待和唤醒必须是同一个锁,不可以对不同锁中的线程进行唤醒,而锁可以是任意对象,所以 ...
- java基础知识回顾之java Thread类--java线程实现常见的两种方式实现Runnable接口(二)
创建线程的第二中方式: /** * 步骤: 1定义类实现Runnable接口 2.实现Runnable接口中的run方法. 3.通过Thread类建立线程对象,并将Run ...
- java基础知识回顾之java集合类-Properties集合
/** java.lang.Object |--java.util.Dictionary<K,V> |--java.util.Hashtable<Object,Obje ...
- java基础知识回顾之java Thread类学习(八)--java.util.concurrent.locks(JDK1.5)与synchronized异同讲解
看API文档介绍几个方法: JDK1.5中提供了多线程的升级解决方案: 特点: 1.将同步synchronized显示的替换成Lock 2.接口Conditio ...
- java基础知识回顾之java Thread类学习(八)--java多线程通信等待唤醒机制经典应用(生产者消费者)
*java多线程--等待唤醒机制:经典的体现"生产者和消费者模型 *对于此模型,应该明确以下几点: *1.生产者仅仅在仓库未满的时候生产,仓库满了则停止生产. *2.消费者仅仅在有产品的时 ...
- java基础知识回顾之java Thread类学习(四)--java多线程安全问题(锁)
上一节售票系统中我们发现,打印出了错票,0,-1,出现了多线程安全问题.我们分析为什么会发生多线程安全问题? 看下面线程的主要代码: @Override public void run() { // ...
随机推荐
- electron加载jquery报错问题
<!-- Insert this line above script imports --> <script>if (typeof module === 'object') { ...
- kaili linux中文乱码
install kaili 用了kali,安装的英文版,一切都很爽,没有乱码 install chrome 不要用apt安装,也不要用chromium, 直接去网上找chrom的deb文件下来安装,注 ...
- --hdu 2570 迷瘴(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2570 Ac code: #include<stdio.h> #include<std ...
- CentOS7 最小化安装后启用无线连接网络
1.ip addr 找到自己的无线网接口 (ps:本人的是wlp2s0) 2.ip link set wlp2s0 up 打开无线网的驱动 3.ip link show wlp2s0 查看该网络接口的 ...
- 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
一. AES对称加密: AES加密 分组 二. 分组密码的填充 分组密码的填充 e.g.: PKCS#5填充方式 三. 流密码: 四. 分组密码加密中的四种模式: 3.1 ECB模式 优点: 1. ...
- (转)DoDataExchange执行时机
void CRegisterDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DAT ...
- __stdcall,__cdecl,__fastcall的区别
__stdcall,__cdecl,__fastcall的区别 标签: dll编译器pascalclassimportinitialization 2009-12-09 15:07 10472人阅读 ...
- 常用的HTTP协议
http协议: 基本格式:http://<host>:<port>/<path>?<query>#<frag> 示例:http://www. ...
- cocos基础教程(12)点击交互的三种处理
1.概述 游戏也好,程序也好,只有能与用户交互才有意义.手机上的交互大致可以分为两部分:点击和输入.其中点击更为重要,几乎是游戏中全部的交互.在Cocos2d-x 3.0中,更改了dispatch机制 ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...