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爬虫老是被封的解决方法【面试必问】
在爬取的过程中难免发生 ip 被封和 403 错误等等,这都是网站检测出你是爬虫而进行反爬措施,在这里为大家总结一下 Python 爬虫动态 ip 代理防止被封的方法. PS:另外很多人在学习Pyth ...
- hive3.1.1 hive-site.xml
<property> <name>hive.metastore.warehouse.dir</name> <value>/data/hive/wareh ...
- Qt应用程序的打包
Windows系统下打包: qt用realse版本编译然后打包发行 debug编译后的文件比realse版本大很多先在编译好的debug或者realse执行程序的目录下将执行该城西对应的所有dell文 ...
- Euler Sums系列(二)
\[\Large\sum_{n=0}^\infty \frac{H_{2n+1}}{(2n+1)^2}=\frac{21}{16}\zeta(3)\] \(\Large\mathbf{Proof:}\ ...
- 树莓派Ubuntu Mate 16.04 修改为国内更新源
收藏:https://blog.csdn.net/wang_shuai_ww/article/details/80386708 更换步骤以root身份打开 /etc/apt/sources.list ...
- 影响IPSec的网络问题
影响IPSec VPN的网络问题:①.动态地址问题:两个 站点之间IPSec VPN的条件是站点之间有固定的IP地址,假如说分支站点采用ADSL上网链路,那么其IP地址是动态的,那么就在VPN时出现问 ...
- Visual Studio 2017安装MSDN
在学习Visual Studio 2017的过程中,总会遇到各种各样的难题,这时候你就会求助书或者是网上大佬们的解释,但是在看视频的过程中,我发现了MSDN这个“好东西”,就立马应用于实践,下面把 ...
- [ DLPytorch ] 注意力机制&机器翻译
MachineTranslation 实现过程 rstrip():删除 string 字符串末尾的指定字符(默认为空格). 语法:str.rstrip([chars]) 参数:chars -- 指定删 ...
- redis在Windows上启动报错
The Windows version of Redis allocates a memory mapped heap for sharing with the forked process used ...
- 虚拟机下修改ip配置
// centos ip 配置vim /etc/sysconfig/network-scripts/ifcfg-eth0 // 虚拟机下删除里面的内容vim /etc/udev/rules.d/70- ...