java join()源码_JAVA join()方法

TestDemo 类

class BThread extends Thread {

public BThread() {

super("[BThread] Thread");

};

public void run() {

String threadName = Thread.currentThread().getName();

System.out.println(threadName + " start.");

try {

for (int i = 0; i < 5; i++) {

System.out.println(threadName + " loop at " + i);

Thread.sleep(1000);

}

System.out.println(threadName + " end.");

} catch (Exception e) {

System.out.println("Exception from " + threadName + ".run");

}

}

}

class AThread extends Thread {

BThread bt;

public AThread(BThread bt) {

super("[AThread] Thread");

this.bt = bt;

}

public void run() {

String threadName = Thread.currentThread().getName();

System.out.println(threadName + " start.");

try {

bt.join();

System.out.println(threadName + " end.");

} catch (Exception e) {

System.out.println("Exception from " + threadName + ".run");

}

}

}

public class TestDemo {

public static void main(String[] args) {

String threadName = Thread.currentThread().getName();

System.out.println(threadName + " start.");

BThread bt = new BThread();

AThread at = new AThread(bt);

try {

bt.start();

Thread.sleep(2000);

at.start();

at.join();

} catch (Exception e) {

System.out.println("Exception from main");

}

System.out.println(threadName + " end!");

}

}

打印结果:

main start. //主线程起动,因为调用了at.join(),要等到at结束了,此线程才能向下执行。

[BThread] Thread start.

[BThread] Thread loop at 0

[BThread] Thread loop at 1

[AThread] Thread start. //线程at启动,因为调用bt.join(),等到bt结束了才向下执行。

[BThread] Thread loop at 2

[BThread] Thread loop at 3

[BThread] Thread loop at 4

[BThread] Thread end.

[AThread] Thread end. // 线程AThread在bt.join();阻塞处起动,向下继续执行的结果

main end! //线程AThread结束,此线程在at.join();阻塞处起动,向下继续执行的结果。

修改一下代码:

1 public classTestDemo {2 public static voidmain(String[] args) {3 String threadName =Thread.currentThread().getName();4 System.out.println(threadName + " start.");5 BThread bt = newBThread();6 AThread at = newAThread(bt);7 try{8 bt.start();9 Thread.sleep(2000);10 at.start();11 //at.join();//在此处注释掉对join()的调用

12 } catch(Exception e) {13 System.out.println("Exception from main");14 }15 System.out.println(threadName + " end!");16 }17 }

打印结果:

main start. // 主线程起动,因为Thread.sleep(2000),主线程没有马上结束;

[BThread] Thread start. //线程BThread起动

[BThread] Thread loop at 0

[BThread] Thread loop at 1

main end! // 在sleep两秒后主线程结束,AThread执行的bt.join();并不会影响到主线程。

[AThread] Thread start. //线程at起动,因为调用了bt.join(),等到bt结束了,此线程才向下执行。

[BThread] Thread loop at 2

[BThread] Thread loop at 3

[BThread] Thread loop at 4

[BThread] Thread end. //线程BThread结束了

[AThread] Thread end. // 线程AThread在bt.join();阻塞处起动,向下继续执行的结果

五、从源码看join()方法

在AThread的run方法里,执行了bt.join();,进入看一下它的JDK源码:

public final void join() throws InterruptedException {

join(0L);

}

然后进入join(0L)方法:

public final synchronized void join(long l)

throws InterruptedException

{

long l1 = System.currentTimeMillis();

long l2 = 0L;

if(l < 0L)

throw new IllegalArgumentException("timeout value is negative");

if(l == 0L)

for(; isAlive(); wait(0L));

else

do

{

if(!isAlive())

break;

long l3 = l - l2;

if(l3 <= 0L)

break;

wait(l3);

l2 = System.currentTimeMillis() - l1;

} while(true);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值