Wait () / wait (timeout) / sleep (wait)?

What is the difference between wait () and wait (timeout). It is expected that wait () should wait for a notification call, but why do we wait (timeout)?

What is the difference between sleep (timeout) and wait (timeout)?

+3
source share
4 answers

wait (timeout) will return if the thread is still waiting after the timeout expires. This refers to a freeze notification, for polling low-power users, etc. Sleep (timeout) will not wake up before the timeout expires; wait (timeout) - either a call to notify () or a timeout, whichever comes first.

Quote from JavaDoc:

( T) , , . T , :

  • , T , .
  • notifyAll .
  • T.
  • . - , .
+10

wait (timeout): , (.. ).

sleep (timeout): sleep - - . , .

, , , ( , , -). , .

+5

wait() wait(timeout) , wait() notify(). wait(timeout) > 0 "", notify() .

+2

, . , , :

  • , , , sleep (t).

  • 2 ( ) , 1- , , - , , 1- , wait()/notify ( ) , .

  • 2, - , (), wait (t) ().

, , wait (t) sleep (t) - , 1, .

0
source

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


All Articles