-2

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 between the sleep and join method. But I would like to know different scenarios where the sleep and join methods could be used interchangeably . According to my thinking following code should work just in the same way. I have a main thread in which I start a new thread (just 1) and I want the main thread to wait for the new thread to complete for some amount of time. The code is as follows:

   newThread.join(10000)

OR,

  Thread.sleep(10000)

but , when I run this code I don't get expected results. why is this so? technically it should be the same right?

user3686864
  • 337
  • 2
  • 5
  • 13
  • 3
    "According to my thinking following code should work just in the same way." - Why would it? Why dont you read the javadoc? sleep and join have completely different purposes. – f1sh Jun 02 '14 at 07:44
  • 2
    Don't be afraid to read the documentation before posting. It can save you and others time in the long run. – Matt Coubrough Jun 02 '14 at 07:46
  • yes , i agree with you, but if i guarantee that the newThread runs for more than 10,000ms then they should work in exactly the same manner , right? (from what I could understand from javadoc) I am just trying new things and got stuck here – user3686864 Jun 02 '14 at 07:56

2 Answers2

6

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.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • yes , I guarantee that the newThread runs for more than 10,000ms, but still I have different outputs. – user3686864 Jun 02 '14 at 07:53
  • while with sleep , it runs waits for the specified time , but with join it keeps on running , it doesn't stop – user3686864 Jun 02 '14 at 07:54
  • @user3686864 without more code I cannot provide any further advice. You sure your `newThread` is running when you call `join`? – Boris the Spider Jun 02 '14 at 07:55
  • Also sleep() keeps the lock on the object. – TheLostMind Jun 02 '14 at 08:04
  • @user3686864 And if you call `newThread.isActive()` immediately after the `join` presumably it returns `false`? So, next question, you sure `newThread` doesn't throw an `Exception`? By guarantee, I meant _really_ guarantee... – Boris the Spider Jun 02 '14 at 08:26
  • yes , it does not throw an exception , see what i am trying is the CalculatePrimes program in http://www.ibm.com/developerworks/java/tutorials/j-threads/j-threads.html , so in the main method i am switching sleep with the join hoping that they will end exactly with the same o/p , so by looking at the code you can see that the thread is guaranteed to execute for more than 10,000 ms – user3686864 Jun 02 '14 at 08:34
  • with the exactly same o/p means end with more or less same o/p (running for 10,000ms) only . But the join version goes on and on and on – user3686864 Jun 02 '14 at 08:35
  • I took the o/p of the mentioned prog in to a text file (both join and sleep), with sleep running the file was roughly 96Mbs and with join I had to externally stop the prog using Ctrl+c and the size exceeded 1.1 GB , (in the o/p I printed more things , so the exact size does not matter , but the difference matters) – user3686864 Jun 02 '14 at 08:37
  • How have you changed the linked code _exactly_. Please edit your answer. – Boris the Spider Jun 02 '14 at 08:41
  • instead of Thread.sleep(TEN_SECONDS); i have written calculator.join(TEN_SECONDS); – user3686864 Jun 02 '14 at 08:43
  • Works perfectly for me - you must have done something else. Post your code if you want real help. I am bored of this pointless guessing. – Boris the Spider Jun 02 '14 at 08:46
1

It's not clear to me what your actual question is, but your third sentence in says, "I would like to know different scenarios where the sleep and join methods could be used interchangeably."

From a practical point of view, if you worked with a team of software developers writing production code, there would be no scenarios in which other developers would permit you to use join(long) as an alternative to sleep(long). No way, no how! It does not matter that there are circumstances under which it would actually work.

Production code should be readable. The intent of the code should be obvious to others. It should obey The Principle of Least Surprise (See Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin). That means, when you write foobar.join(n), you should be expecting the foobar thread to die. You might be prepared to handle the timeout case, but that should be the exception, not the rule. There is no other legitimate reason to call join(n). Anything else would be a "hack", and we do not put hacks in production code.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57