Similarity between sleep and join in java

前端 未结 2 864
忘了有多久
忘了有多久 2021-01-29 12:12

As the question suggests , I want to know the similarity between the sleep and join methods on the thread. I have gone through many questions that describe the difference betwee

2条回答
  •  既然无缘
    2021-01-29 12:34

    NO NO NO

    sleep and join are completely different.

    join will wait for the specified Thread to finish (either normally or abnormally) or until the time expires.

    sleep will simply stop the current thread for the specified time.

    They are completely different. One explicitly waits for another Thread and wakes the instant that that Thread ends. sleep just stops execution.

    If you can guarantee than newThread will take longer then 10,000ms to to complete then they become equivalent, but this is a degenerate case.

    If you want to wait for another Thread to complete use join.

    If you want your current Thread to stop what it's doing and sleep for a while use sleep.

提交回复
热议问题