日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java join 源码_java并发:join源码分析

發布時間:2024/9/27 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java join 源码_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 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;

@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 源码_java并发:join源码分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。