join

join

join是Thread方法,它的作用是A线程中子线程B在运行之后调用了B.join(),A线程会阻塞直至B线程执行结束

join源码(只有继承Thread类才能使用)

基于openjdk1.8的源码

    public final void join() throws InterruptedException {join(0);}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) {while (isAlive()) {wait(0);}} else {while (isAlive()) {long delay = millis - now;if (delay <= 0) {break;}wait(delay);now = System.currentTimeMillis() - base;}}}/*** Tests if this thread is alive. A thread is alive if it has* been started and has not yet died.** @return  <code>true</code> if this thread is alive;*          <code>false</code> otherwise.*/public final native boolean isAlive();/* <p>* Note that the {@code wait} method, as it places the current thread* into the wait set for this object, unlocks only this object; any* other objects on which the current thread may be synchronized remain* locked while the thread waits.* <p>...*/public final native void wait(long timeout) throws InterruptedException;

源码分析

A线程调用了B.join(),获取了B的锁,当B alive,B.wait(0)会让当前线程A阻塞,执行join方法等同于,A线程进入了下列
的语句

syncronized(B){
...
B.wait
...
}

代码测试

package com.java.javabase.thread.base;import lombok.extern.slf4j.Slf4j;@Slf4j
public class JoinTest {public static void main(String[] args) {Thread t1 =new ThreadOne("t1");t1.start();log.info("current thread is : {} run",Thread.currentThread().getName());try {t1.join();} catch (InterruptedException e) {log.info("InterruptedException",e);e.printStackTrace();}log.info("current thread is : {} end",Thread.currentThread().getName());}static class  ThreadOne extends Thread{public ThreadOne(String name){super(name);}@Overridepublic  void run(){log.info("current thread is : {} start",Thread.currentThread().getName());for(int i =0;i<10;i++){log.info("current thread is : {} run",Thread.currentThread().getName());}log.info("current thread is : {} end",Thread.currentThread().getName());}}
}

说明

主线程调用t1.join之后,主线程只有t1的锁进入阻塞状态

运行结果

2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 start
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run
2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 end
2019-07-29 20:14:21,551   [main] INFO  JoinTest  - current thread is : main run
2019-07-29 20:14:21,551   [main] INFO  JoinTest  - current thread is : main end

转载于:https://www.cnblogs.com/JuncaiF/p/11269934.html

java并发:join源码分析相关推荐

  1. Java并发-ReentrantReadWriteLock源码分析

    ReentrantLock实现了标准的互斥重入锁,任一时刻只有一个线程能获得锁.考虑这样一个场景:大部分时间都是读操作,写操作很少发生:我们知道,读操作是不会修改共享数据的,如果实现互斥锁,那么即使都 ...

  2. java join 源码_java并发:join源码分析

    join join join是Thread方法,它的作用是A线程中子线程B在运行之后调用了B.join(),A线程会阻塞直至B线程执行结束 join源码(只有继承Thread类才能使用) 基于open ...

  3. 喻红叶《Java并发-ReentrantReadWriteLock源码分析》

    ReentrantLock实现了标准的互斥重入锁,任一时刻只有一个线程能获得锁.考虑这样一个场景:大部分时间都是读操作,写操作很少发生:我们知道,读操作是不会修改共享数据的,如果实现互斥锁,那么即使都 ...

  4. 并发编程5:Java 阻塞队列源码分析(下)

    上一篇 并发编程4:Java 阻塞队列源码分析(上) 我们了解了 ArrayBlockingQueue, LinkedBlockingQueue 和 PriorityBlockingQueue,这篇文 ...

  5. java.util.ServiceLoader源码分析

    java.util.ServiceLoader源码分析 回顾: ServiceLoader类的使用(具体参考博客http://blog.csdn.net/liangyihuai/article/det ...

  6. Java集合类框架源码分析 之 LinkedList源码解析 【4】

    上一篇介绍了ArrayList的源码分析[点击看文章],既然ArrayList都已经做了介绍,那么作为他同胞兄弟的LinkedList,当然必须也配拥有姓名! Talk is cheap,show m ...

  7. 多线程高并发编程(8) -- Fork/Join源码分析

    一.概念 Fork/Join就是将一个大任务分解(fork)成许多个独立的小任务,然后多线程并行去处理这些小任务,每个小任务处理完得到结果再进行合并(join)得到最终的结果. 流程:任务继承Recu ...

  8. Java容器 | 基于源码分析List集合体系

    一.容器之List集合 List集合体系应该是日常开发中最常用的API,而且通常是作为面试压轴问题(JVM.集合.并发),集合这块代码的整体设计也是融合很多编程思想,对于程序员来说具有很高的参考和借鉴 ...

  9. java 并发框架源码_某网Java并发编程高阶技术-高性能并发框架源码解析与实战(云盘下载)...

    第1章 课程介绍(Java并发编程进阶课程) 什么是Disruptor?它一个高性能的异步处理框架,号称"单线程每秒可处理600W个订单"的神器,本课程目标:彻底精通一个如此优秀的 ...

最新文章

  1. POJ-1094 Sorting it All Out
  2. 知识图谱基础知识之一——人人都能理解的知识图谱
  3. IntelliJ 启动不同端口的两个spring cloud项目
  4. php识别中文编码并自动转换为UTF-8
  5. 封装直传阿里云存储文件上传控件
  6. win10共享打印错误0x0000006_Win7打印机无法共享提示错误代码0x000006d9的解决方法...
  7. 【oracle】除数为0
  8. Vector Math for 3D Computer Graphics
  9. LeetCode 4Sum 4个数之和
  10. 性能优化-内存泄露常见例子
  11. 解决windows10 9926版本中无法访问samba的方法
  12. 26_多易教育之《yiee数据运营系统》数据治理-atlas介绍篇
  13. 面部表情识别---学习笔记
  14. vue中slot-scop=“scope“
  15. prometheus +granfana监控告警
  16. Matlab求解阶跃响应性能指标 (上升时间、调整时间、峰值时间、超调量)
  17. Python创建进程的四种方式
  18. 计算机专业浅谈:都说这行业35岁失业,是真的嘛?
  19. window10安装cloc
  20. 编辑数学公式绘制曲线

热门文章

  1. php 非数字和字母,如何通过非数字与字符的方式实现PHP WebShell详解
  2. 十六、在屏幕上显示时间
  3. 构建高性能WEB站点笔记二
  4. SQLSERVER:sqlserver2008r2安装好后,自动提示功能不可以使用
  5. 2014校招 百度试题及答案
  6. 黄冈中学首页布局(自己做的)
  7. 十字路口待转区什么用_左转待转区,到底怎样掉头?
  8. (45)FPGA面试题格雷码特点及其应用
  9. html5设置播放按钮,HTML5 Flowplayer的附加播放按钮
  10. python怎么打开笔记本无线网络开关_如何轻松搞定 笔记本搜不到WIFI信号问题