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是一个计数信号量,它的本质是一个"共享锁". 信号量维护了 ...
随机推荐
- JS中的数组创建,初始化
JS中没有专门的数组类型.但是可以在程序中利用预定义的Array对象及其方法来使用数组. 在JS中有三种创建数组的方法: var arr = new Array(1,2,3,4); var arr = ...
- IE浏览器清浮动
.cfx:after,.cfx:before{content:" ";display:table;}.cfx:after{clear:both;}.cfx{*zoom:1;} 今天 ...
- 关于报错:Warning: Cannot modify header information - headers already sent by (output started at
8月5日,第一个项目即将完成,测试时,发现登录功能会出现小问题:记住密码的时候会报错 Warning: Cannot modify header information - headers alrea ...
- 《Web安全攻防 渗透测试实战指南 》 学习笔记 (五)
Web安全攻防 渗透测试实战指南 学习笔记 (五) 第四章 Web安全原理解析 (一) (一)SQL注入的原理 1.web应用程序对用户输入数据的合法性没有判断. 2.参数用户可控:前端传给 ...
- 创建mysql数据库,在新数据库中创建表,再尝试删除表
创建之前,先登录数据库存 mysql -u 账号 -p密码 登录完成后,展示一下已存在的数据库 show databases; 创建数据库 create database test111; 然后展示一 ...
- docker运行安装mysql postgres
安装mysql [root@host1 ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/mysql 5.7 4d ...
- RAID0---RAID10(重点)
二.基本原理 RAID ( Redundant Array of Independent Disks )又叫独立磁盘冗余阵列,通常简称为磁盘阵列. RAID是一种把多块独立的硬盘(物理硬盘)按不同方式 ...
- 【Python矩阵及其基础操作】【numpy matrix】
一.矩阵生成 1.numpy.matrix: import numpy as np x = np.matrix([ [1, 2, 3],[4, 5, 6] ]) y = np.matrix( [1, ...
- Excel实用知识1
纯手打,可能有错别字,使用的版本是office2013 转载请注明出处 http://www.cnblogs.com/hnnydxgjj/p/6329509.html ,谢谢 使用现成的模板 ”开头的 ...
- Red Black Tree(红黑树)
(修改于 2018-05-06 15:53:22 还差删除维护操作.层序遍历没完成.维护操作没完成不想写层序遍历怎么办...) 今天下午完成了红黑树的插入的维护操作,但删除的维护操作还没有解决,删除的 ...