博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java并发:join源码分析
阅读量:4349 次
发布时间:2019-06-07

本文共 3724 字,大约阅读时间需要 12 分钟。

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  true if this thread is alive;     *          false otherwise.     */    public final native boolean isAlive();     /* 

* 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. *

... */ 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;@Slf4jpublic 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 start2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 run2019-07-29 20:14:21,551   [t1] INFO  JoinTest  - current thread is : t1 end2019-07-29 20:14:21,551   [main] INFO  JoinTest  - current thread is : main run2019-07-29 20:14:21,551   [main] INFO  JoinTest  - current thread is : main end

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

你可能感兴趣的文章
【兼容性】IE不支持日期字符串转换为日期对象
查看>>
函数语言
查看>>
log4j.properties路径修改后web.xml配置
查看>>
笔试编程---快手实习题目
查看>>
csp20170304地铁修建_Solution
查看>>
快速沃尔什变换 与 快速莫比乌斯变换
查看>>
SQL的四种连接-左外连接、右外连接、内连接、全连接
查看>>
Palindromic Substrings
查看>>
thinkphp-调试模式
查看>>
改变和恢复view的方向
查看>>
C#调用金数据API
查看>>
[UIBarButtonItem alloc] initWithImage:颜色被冲的解决办法
查看>>
Django 第八课 5.【数据库的Connection、Cursor】
查看>>
论时间与前进
查看>>
iOS录制语音and播放语音开发
查看>>
gdal vs2013编译
查看>>
Java软件工程师面试常见问题集锦之一
查看>>
MySql创建视图
查看>>
记录使用yum安装nginx之后的目录问题
查看>>
【QT学习笔记】二、信号槽和自定义信号槽
查看>>