youyichannel

志于道,据于德,依于仁,游于艺!

0%

JUC初探-07

Thread#join

/**
* Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
*
* <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.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws InterruptedException
* if any thread has interrupted the current thread. The
* <i>interrupted status</i> of the current thread is
* cleared when this exception is thrown.
*/
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;
}
}
}

1)是否释放锁

Thread#join()方法,从代码中可以看出调用的是wait(0)方法。

需要注意:这个wait方法是Object的方法,但是因为Object是父类,其实此处的wait()方法有一个隐含的意义:当前的线程类的wait方法。因此是当前线程释放了CPU,而且是当前线程(Thread类)实例对象释放了锁。

public class JoinReleaseLock {

static Object lock = new Object();

public static void main(String[] args) throws InterruptedException {
for (int i = 1; i <= 2; i++) {
Thread thread = new Thread(new SubThread(), "Thread - " + i);
thread.start();
Thread.sleep(100);
}
}

static class SubThread implements Runnable {

@Override
public void run() {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + "获取到锁");
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}

上述代码的Thread-1线程被Blocked。

Thread的join方法释放的是当前调用join方法的线程实例对象的锁。

synchronized (obj) {
thread.join(); // 不释放当前同步代码块的锁
}

synchronized (thread) {
thread.join(); // 释放当前同步代码块的锁
}

具体要看当前锁的对象是谁,如果是调用join方法的对象是当前锁的对象,则会释放锁。

2)是否对中断敏感

if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

3)是否释放CPU

,底层调用了wait方法。

资料:https://www.bilibili.com/video/BV1HN4y1m7Sh/ 强推 河北王校长