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是一个计数信号量,它的本质是一个"共享锁". 信号量维护了 ...
随机推荐
- python基础(一)--python介绍
1. Python语言 1.1 编程语言 语言是人类最重要的交际工具,是人类之间进行信息交换的主要表达方式. 编程语言是用来定义计算机程序的语言,用来向计算机发出指令. 1.2 Python语言 Py ...
- Flask - g变量
传送门 http://flask.pocoo.org/docs/1.0/appcontext/#storing-data http://flask.pocoo.org/docs/1.0/appcont ...
- Informatica PowerCenter 常用转换组件一览表
原文地址:https://blog.csdn.net/yongjian1092/article/details/52176018 转换类型: 积极转换(Active):可以更改通过它来传递的数据行数, ...
- BUG搬运工:CSCun88303-CIMC Memory Leak : Can't SSH/HTTP to CIMC
Symptom:Unable to SSH/HTTP to the CIMC of the C-series server, however the CIMC can be pinged. Also ...
- BFS迷宫问题
链接:https://ac.nowcoder.com/acm/challenge/terminal来源:牛客网 小明现在在玩一个游戏,游戏来到了教学关卡,迷宫是一个N*M的矩阵. 小明的起点在地图中用 ...
- 利用ProxySQL实现MySQL的读写分离
本文简单介绍ProxySQL的安装及如果实现后端MySQL主从结构的读写分离. 一.ProxySQL安装 Proxy官方地址:https://proxysql.com/ proxysql-2.0.8- ...
- Session服务器之Memcached
材料:两台Tomcat(接Session复制一起做) 第一台Tomcat:IP为130 [root@localhost ~]# yum install libevent memcached -y ...
- Linux 命令中 more、less、head、tail 命令的用法
more 命令 more 命令,功能类似 cat ,cat 命令是将整个文件的内容从上到下显示在屏幕上. more 命令会一页一页的显示,方便使用者逐页阅读,而最基本的指令就是按空白键(space)往 ...
- MySQL报Too many connections
错误信息 Exception in thread "main" java.sql.SQLNonTransientConnectionException: Data source r ...
- kafka2x-Elasticsearch 数据同步工具demo
Bboss is a good elasticsearch Java rest client. It operates and accesses elasticsearch in a way simi ...