[英]how to notify lock when calling thread.join()
Simply describe 简单描述
public static void main(String[] args ){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread stopped");
}
});
try {
t.start();
t.join();
System.out.println("task completed");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
t.join()
The main thread waits for its subthread t to die. t.join()
主线程等待其子线程t死亡。 the call stack of t.join()
just like t.join()
的调用堆栈就像
join -> join(0)
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;
}
}
}
The main locked the object t and waited for notification. 主体锁定对象t并等待通知。 I have some confusion when and where the notify be called.
我在何时何地调用通知时有些困惑。 Does the native function
stop0()
call it? 原生函数
stop0()
调用它吗?
Take a look at this: who and when notify the thread.wait() when thread.join() is called? 看一下: 谁以及何时在调用thread.join()时通知thread.wait()?
After run() finishes, notify() is called by the Thread subsystem. run()完成之后,Thread子系统将调用notify()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.