Who and when notifies thread.wait () when thread.join () is called?

thread.join() will call thread.wait() , but who and when notifies (either with thread.notify() or notifyAll() ) thread.wait() ?

As you know, thread pooling will wait for the thread to finish, but who calls it notification?

+3
source share
2 answers

Edit:

Oh, you're talking about the innermost Thread object. Inside join() we see a wait() . Sort of:

 while (isAlive()) { wait(0); } 

notify() for this is handled by the Thread subsystem. When the run() method completes, notify() is called on the Thread object. I am not sure if it is possible to see the code that actually calls notify() - this is apparently executed in the native code.


No user code should call notify() for this Thread object. Java Thread code handles this internally. Once the thread ends, the call to join() will return.

For example, the following code will run fine, and the join() call will be canceled without notify() or notify() .

 Thread thread = new Thread(new Runnable() { public void run() { // no-op, just return immediately } }); thread.start(); thread.join(); 

It is important to note that this behavior should probably not be relied upon. The call to notify() is internal to the notify() system. You should use join() if you expect the thread to complete.

+7
source

As for jdk7 for linux, you can get the answer from openjdk source code.

/jdk7/hotspot/src/os/linux/vm/os_linux.cpp

 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread); static void *java_start(Thread *thread) { ... thread->run(); return 0; } 

and when the thread starts in java, the thread will be an instance of JavaThread.

/jdk7/hotspot/src/share/vm/runtime/thread.cpp

 void JavaThread::run() { ... thread_main_inner(); } void JavaThread::thread_main_inner() { ... this->exit(false); delete this; } void JavaThread::exit(bool destroy_vm, ExitType exit_type) { ... // Notify waiters on thread object. This has to be done after exit() is called // on the thread (if the thread is the last thread in a daemon ThreadGroup the // group should have the destroyed bit set before waiters are notified). ensure_join(this); ... } static void ensure_join(JavaThread* thread) { // We do not need to grap the Threads_lock, since we are operating on ourself. Handle threadObj(thread, thread->threadObj()); assert(threadObj.not_null(), "java thread object must exist"); ObjectLocker lock(threadObj, thread); // Ignore pending exception (ThreadDeath), since we are exiting anyway thread->clear_pending_exception(); // Thread is exiting. So set thread_status field in java.lang.Thread class to TERMINATED. java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED); // Clear the native thread instance - this makes isAlive return false and allows the join() // to complete once we've done the notify_all below java_lang_Thread::set_thread(threadObj(), NULL); lock.notify_all(thread); // Ignore pending exception (ThreadDeath), since we are exiting anyway thread->clear_pending_exception(); } 

therefore lock.notify_all (thread) will notify all threads awaiting thread completion.

+1
source

Source: https://habr.com/ru/post/907004/


All Articles