java thread 解析,浅谈Java线程Thread.join方法解析

本文详细解析了Java中Thread的join方法,说明了其如何通过wait和notifyAll实现线程间的同步。当一个线程调用另一个线程的join方法时,它将等待该线程执行完毕后再继续执行。join方法有两种形式,一种不带参数,另一种接受一个时间参数,超过指定时间后,调用线程不再等待。通过示例展示了join方法的使用效果,帮助理解线程同步的概念。
摘要由CSDN通过智能技术生成

join字面上是加入的意思,我们先看看join方法的解释和实现。

/**

* Waits for this thread to die.

* 调用方线程(调用join方法的线程)执行等待操作,直到被调用的线程(join方法所属的线程)结束,再被唤醒

*

An invocation of this method behaves in exactly the same

* way as the invocation

*

*

* @throws InterruptedException

* if any thread has interrupted the current thread. The

* interrupted status of the current thread is

* cleared when this exception is thrown.

*/

public final void join() throws InterruptedException {

join(0);

}

这里join是调用的

/**

* Waits at most {@code millis} milliseconds for this thread to

* die. A timeout of {@code 0} means to wait forever.

* 等待线程执行结束,或者指定的最大等待时间到了,调用方线程再次被唤醒,如果最大等待时间为0,则只能等线程执行结束,才能被唤醒。

*

This implementation uses a loop of {@code this.wait} calls

* conditioned on {@code this.isAlive}. As a thread terminates the

* {@code this.notifyAll} method is invoked. It is recommended that

* applications not use {@code wait}, {@code notify}, or

* {@code notifyAll} on {@code Thread} instances.

*

*

*/

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;

}

}

}

可以看到,join方法本身是通过wait方法来实现等待的,这里判断如果线程还在运行中的话,则继续等待,如果指定时间到了,或者线程运行完成了,则代码继续向下执行,调用线程就可以执行后面的逻辑了。

但是在这里没有看到哪里调用notify或者notifyAll方法,如果没有调用的话,那调用方线程会一直等待下去,那是哪里调用了唤醒它的方法呢?通过查证得知,原来在线程结束时,java虚拟机会执行该线程的本地exit方法,

//线程退出函数:

void JavaThread::exit(bool destroy_vm, ExitType exit_type) {

...

//这里会处理join相关的销毁逻辑

ensure_join(this);

...

//处理join相关的销毁逻辑

static void ensure_join(JavaThread* thread) {

Handle threadObj(thread, thread->threadObj());

ObjectLocker lock(threadObj, thread);

thread->clear_pending_exception();

java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);

java_lang_Thread::set_thread(threadObj(), NULL);

//这里就调用notifyAll方法,唤醒等待的线程

lock.notify_all(thread);

thread->clear_pending_exception();

}

这样线程什么时候被唤醒就明白了。下面写个例子看下效果。

public class JoinTest {

public static void main(String[] args) {

ThreadBoy boy = new ThreadBoy();

boy.start();

}

static class ThreadBoy extends Thread{

@Override

public void run() {

System.out.println("男孩和女孩准备出去逛街");

ThreadGirl girl = new ThreadGirl();

girl.start();

try {

girl.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("男孩和女孩开始去逛街了");

}

}

static class ThreadGirl extends Thread{

@Override

public void run() {

int time = 5000;

System.out.println("女孩开始化妆,男孩在等待。。。");

try {

Thread.sleep(time);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("女孩化妆完成!,耗时" + time);

}

}

}

执行结果为:

男孩和女孩准备出去逛街

女孩开始化妆,男孩在等待。。。

女孩化妆完成!,耗时5000

男孩和女孩开始去逛街了

就是男孩和女孩准备去逛街,女孩要化妆先,等女孩化妆完成了,再一起去逛街。

那join(time)的用法是怎么样的呢?

public class JoinTest {

public static void main(String[] args) {

ThreadBoy boy = new ThreadBoy();

boy.start();

}

static class ThreadBoy extends Thread{

@Override

public void run() {

System.out.println("男孩和女孩准备出去逛街");

ThreadGirl girl = new ThreadGirl();

girl.start();

int time = 2000;

try {

girl.join(time);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("男孩等了" + time + ", 不想再等了,去逛街了");

}

}

static class ThreadGirl extends Thread{

@Override

public void run() {

int time = 5000;

System.out.println("女孩开始化妆,男孩在等待。。。");

try {

Thread.sleep(time);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("女孩化妆完成!,耗时" + time);

}

}

}

这里仅仅把join方法换成了join(time)方法,描述改了点,打印的结果是:

男孩和女孩准备出去逛街

女孩开始化妆,男孩在等待。。。

男孩等了2000, 不想再等了,去逛街了

女孩化妆完成!,耗时5000

男孩等了join(time)中的time时间,如果这个time时间到达之后,女孩所在的线程还没执行完,则不等待了,继续执行后面的逻辑,就是不等女孩了,自己去逛街。

由此看出,join方法是为了比较方便的实现两个线程的同步执行,线程1执行,碰到线程2后,等待线程2执行后,再继续执行线程1的执行,加入的意思现在就比较形象化了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持聚米学院。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值