5

java | join 原理

 1 year ago
source link: https://benpaodewoniu.github.io/2022/12/18/java111/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

java | join 原理

2022-12-18java进阶多线程进阶

1

join 是等待线程终止。它的原理和 java | 设计模式 同步模式-保护性暂停 优化 超时 等待线程超时是一样的。

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;
}
}
}

java | 设计模式 同步模式-保护性暂停 优化 超时 是一样的原理。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK