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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值