-9

I wanted to know the use of join(long millis) over sleep(long millis), as both of them will pause that thread for some time duration.

So if thread which joined by another thread will still invoked after that time duration, even joined thread completed or not. Then why to use join with milli's instead of sleep with milli's.

Though, I have received answer for this question just want to explain the question clearly.

Thanks,

  • 2
    please read the docs, the two methods do completely different things. – luk2302 Sep 08 '17 at 10:46
  • 2
    [The difference is clearly explained in the Javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html). If you are confused, please [edit] your question to describe your confusion. – Andy Turner Sep 08 '17 at 10:46
  • Yes Sir, I am getting your point but as mentioned on doc. Both are stopping the thread for time duration. I know in case of join it waits for the joined thread to die within that duration of time. But by using sleep we can also make a thread to wait for that certain time. Then why to use join with milliseconds, if we don't depend on that thread to complete its work. – Vindhya Pratap Singh Sep 08 '17 at 10:50
  • @VindhyaPratapSingh so you know what the difference is. ``sleep`` just pauses the current thread, ``join`` waits for the completion of a different thread. If you don't depend on the other thread, don't use join. What is your question? – f1sh Sep 08 '17 at 10:54
  • I find this question in fact a juwel. Hopefully no interviewer collects it. – Joop Eggen Sep 08 '17 at 10:55
  • Pardon my English this line " If you don't depend on the other thread, don't use join." conveys wrong meaning. I meant to say that when we join a thread with mills the thread that joined another thread will be invoked again after that time, even joined thread completed or not. So why to used this join with milliseconds? why not sleep? – Vindhya Pratap Singh Sep 08 '17 at 11:03
  • 1
    because *if* the other thread finishes faster then you dont wait some extra time during which you should continue your work. – luk2302 Sep 08 '17 at 11:05
  • @luk2302 Thanks that make sense. – Vindhya Pratap Singh Sep 08 '17 at 11:07
  • 1
    I think you asked the wrong question: IMO, you should think of `t.join()` and `t.join(n)` as _one_ function that may be called with or without an optional timeout argument. Then ask, "why would somebody want the version with the timeout argument?" – Solomon Slow Sep 08 '17 at 13:56
  • @James Yes you are right, this is exactly what I want to know. But I was failed to explain that question in right way, as you did. Now I am blocked and cannot ask anymore got punished by SO :) – Vindhya Pratap Singh Sep 08 '17 at 19:11

1 Answers1

0

Assuming you have one thread: use sleep(timeout) - it will always wait timeout seconds before continuing.

Assuming you have two threads:

  • Option 1: Thread1 simply should wait and has nothing to do with Thread2 - use sleep(timeout) - it will always wait timeout seconds before continuing.
  • Option 2: Thread1 does have something to do with Thread2 and therefore (and only therefore) calls join(timeout) on Thread2.
    • Option 2.1: Thread2 finishes before the timeout. Thread1 can immediately continue its processing knowing that Thread2 has completed. It will not wait as many as timeout seconds.
    • Option 2.2: Thread2 finishes after the timeout. Thread1 now knows that Thread2 is still doing some work and can maybe show the user "the process is taking a bit longer" or even cancel the other task. At this point it will waited timeout seconds. This behaves like sleep but you do not know beforehand wether 2.1 or 2.2 will happen, therefore sleep is not a proper alternative.
luk2302
  • 55,258
  • 23
  • 97
  • 137