java并发:join源码分析
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);
}
@Override
public 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
java并发:join源码分析的更多相关文章
- 细说并发5:Java 阻塞队列源码分析(下)
上一篇 细说并发4:Java 阻塞队列源码分析(上) 我们了解了 ArrayBlockingQueue, LinkedBlockingQueue 和 PriorityBlockingQueue,这篇文 ...
- Java split方法源码分析
Java split方法源码分析 public String[] split(CharSequence input [, int limit]) { int index = 0; // 指针 bool ...
- 【JAVA】ThreadLocal源码分析
ThreadLocal内部是用一张哈希表来存储: static class ThreadLocalMap { static class Entry extends WeakReference<T ...
- 【Java】HashMap源码分析——常用方法详解
上一篇介绍了HashMap的基本概念,这一篇着重介绍HasHMap中的一些常用方法:put()get()**resize()** 首先介绍resize()这个方法,在我看来这是HashMap中一个非常 ...
- 【Java】HashMap源码分析——基本概念
在JDK1.8后,对HashMap源码进行了更改,引入了红黑树.在这之前,HashMap实际上就是就是数组+链表的结构,由于HashMap是一张哈希表,其会产生哈希冲突,为了解决哈希冲突,HashMa ...
- 多线程高并发编程(8) -- Fork/Join源码分析
一.概念 Fork/Join就是将一个大任务分解(fork)成许多个独立的小任务,然后多线程并行去处理这些小任务,每个小任务处理完得到结果再进行合并(join)得到最终的结果. 流程:任务继承Recu ...
- 并发-AtomicInteger源码分析—基于CAS的乐观锁实现
AtomicInteger源码分析—基于CAS的乐观锁实现 参考: http://www.importnew.com/22078.html https://www.cnblogs.com/mantu/ ...
- Java并发包源码分析
并发是一种能并行运行多个程序或并行运行一个程序中多个部分的能力.如果程序中一个耗时的任务能以异步或并行的方式运行,那么整个程序的吞吐量和可交互性将大大改善.现代的PC都有多个CPU或一个CPU中有多个 ...
- Java - "JUC" Semaphore源码分析
Java多线程系列--“JUC锁”11之 Semaphore信号量的原理和示例 Semaphore简介 Semaphore是一个计数信号量,它的本质是一个"共享锁". 信号量维护了 ...
随机推荐
- 将jar包安装到本地Maven仓库
1.安装jar包到本地仓库 mvn install:install-file -Dfile=F:\jiefu\lib\alipay-sdk-java20170324180803.jar -Dgroup ...
- linux理论知识点(用于考试)
ps:为其十天左右的linux培训即将结束了,未雨绸缪,为了更好的通过之后的考试,提前多看些考试题和知识点.这是在chinaunix论坛看到的一个帖子,贴来分享. 原文地址:[http://bbs.c ...
- POJ1087 A Plug of UNIX
你作为某高管去住宿了,然后宾馆里有几种插座,分别有其对应型号,你携带了几种用电器(手机,电脑一类的),也有其对应型号:可是不一定用电器就能和插座匹配上,于是宾馆的商店里提供了一些转换器,这些转换器可以 ...
- python3.8的PySimpleGUI学习的温度转换(℃转℉)
一.代码1: #导出模块 import PySimpleGUI as sg #总体布局,sg.InputText(),默认size=(45,1). layout = [ [sg.Text('Celci ...
- 一个支持国密SM2/SM3/SM4/SM9/ZUC/SSL的密码工具箱
转:https://blog.csdn.net/xuq09/article/details/91815366 The GmSSL Project网址:http://gmssl.org/docs/qui ...
- AireOS WLC配置抓包
这个Note主要列举在AireOS WLC上如何抓包.它实现的步骤也相对比较简单: 1.开启debug packet, 2.有数据被抓取到时,会以16进制的形式在WLC上输出, 3.我们将输出信息保存 ...
- ANSYS 瞬态热分析---零件在水中冷却
目录 1. 案例 2. APDL分析 1. 案例 一个温度为300℃的铜环和一个温度为200℃的铁环,放置到22℃的水中进行淬火.水桶为铁质的圆形.分析中忽略水的流动. 材料参数 热性能 铜 铁 水 ...
- Django继承drf的user模型的demo
1.安装虚拟环境 #mkvirtualenv drfdemo -p python3 #pip install django #pip install djangorestframework #pip ...
- python邮箱发送
普通发送邮件 使用email模块和stmplib模块,内容比较固定,配好了即可实现,代码如下 一.普通邮箱发送 # -*- coding:utf-8-*- import smtplib from em ...
- springmvc_ajax异步上传文件(基于ajaxfileupload.js)
引入js <script th:src="@{/js/ajaxfileupload.js}"></script> html <tr> <t ...