java join()方法

join方法

合并线程?怎末实现?

public final synchronized void join()
public final synchronized void join(long millis)
public static void main(String[] args) {
    // run类实现了runnable接口
    Run r = new Run();

    Thread t1 = new Thread(r, "t1");

    try {
        t1.start();
        // t1.join(); // 相当于t1.join(0); main线程等到t1执行完再往下执行
        // main线程暂停直到200ms或t1运行结束
        t1.join(200);
        System.out.println("t1 end");
    } catch(Exception e) {
        e.printStackTrace();
    }

    System.out.println("main end");

}

class Run2 implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(200);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

它就是提供了这么一个功能,具体实现看不懂,有native方法

// 其中的wait()方法为native方法,正是Object类的wait()方法
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;
        }
    }
}

现在又有一个疑问了,如果是普通对象调用wait()方法,那么访问该对象的线程wait,如果是线程对象调用wait()方法,那么谁wait()呢?main线程?

糊涂了,糊涂了。wait方法和对象无关,和访问对象的线程有关,访问Thread.wait()方法的是main线程,所以wait的是main线程。

既然这样就好理解了

join方法阻塞调用方法的线程,这样就造成了线程合并的假象。实质上就是一个线程的wait。

那么,main线程wait了,怎末没有notify呢?

看了文档才知道

* <p> 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.

join方法调用结束时,notifyAll方法自动被调用。

这样,main线程又能继续执行了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值