ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java并发:join源码分析

2019-07-30 14:52:05  阅读:206  来源: 互联网

标签:20 join 21 thread t1 current 源码 JoinTest java


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

标签:20,join,21,thread,t1,current,源码,JoinTest,java
来源: https://www.cnblogs.com/JuncaiF/p/11269934.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有