Difference between wait (long timeout) and join (long millis)?

Both wait () and join () methods when calling thread-1 in thread-2 cause thread-1 to wait for thread-2 either for a while or until thread-2 is completed.

If we use overloaded versions of these methods, i.e. wait (long timeout) and join (long millis), then

  • In case of waiting (long timeout), stream-1 will become executable either by notification (or notifyall), or even timeout (depending on what will be the first).

  • In the case of a connection (long millis), thread-2 will become executable either upon completion of thread-2 or in a timeout (depending on what comes first).

So what is the difference between these two implementations?

Some of them I thought:

  • For wait () we need to lock the object we are expecting. For join (), this is optional.
  • After executing wait (), the thread deletes the obtained lock and again receives the lock after it is restarted. But what about joining? Does the thread remove the lock after the join is completed, if it was done from a synchronized block (or method)?
+4
source share
1 answer

As you say, the β€œrelease” process is completely different - if it is based on notify() , the other is based on the completion of the stream. These are completely different appeals that serve completely different purposes.

Actually, there are explicit warnings not to call wait() on Thread monitors (although I cannot immediately find these warnings), since the internal Java code acquires locks for them (and uses wait / notify ).

But no, calling join() on Thread does not free the monitor if it owns the current executable thread.

Basically, you should not think of them as similar at all - one of them is waiting for the stream to complete; the other is to wait for coordination of cooperation.

+6
source

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


All Articles