1

I have googled a lot about isAlive vs join(), but didn't get a proper explanation. Suppose I have Thread-1 and Thread-2 and I need to start Thread-2 only after Thread-1 has completed.

The above requirement can be achieved using both the methods.

  1. So what is the difference?
  2. Which method to use in which scenario?
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
user1877246
  • 449
  • 1
  • 6
  • 9
  • http://stackoverflow.com/a/2773506/548036, this post may answer your questions – Mo Patel Dec 27 '13 at 10:23
  • ..or just call the 'thread-2' code at the end of thread-1 function/method, so eliminating thread-2, or start thread 2 at the end of thread 1 function/method. Why do devs. have to make stuff complex when it can be so easy? – Martin James Dec 27 '13 at 11:39

3 Answers3

3

.isAlive() just returns a boolean that indicates whether the thread is alive, the status of the thread - it returns true or false, indicating whether the thread is "done".

.join() makes the current thread you are in wait until the thread that you're calling .join() on has completed. For instance, if you have some calculations that needs to be done in another thread before moving on with the rest of your code in your current thread.

I'm assuming it's the Thread-class from Java you're referring to. Here's a good tutorial for understanding multithreading. It can seem like heavy stuff, but once you get your head around threads, you'll become a much better programmer instantly. It's incredibly useful.

Tobias Roland
  • 1,182
  • 1
  • 13
  • 35
  • 1
    ...or, more likely, isAlive returns true or false, indicating whether the thread was "done" at some time before the call returned. It's a ridiculous feature that just encourages developers to poll continually and continually create/terminate/destroy threads. Just a stupid feature. – Martin James Dec 27 '13 at 11:42
0

join blocks the current running thread and execute other thread and when it finishes it then resume the thread that was blocked.

and i think isAlive just return whether thread is running or not.

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • isAlive just returns whether thread was running or not - a 'true' return does not mean that the thread still exists. – Martin James Dec 27 '13 at 11:55
-1

'Which method to use in which scenario?' what scenario?

In general, using neither would be a good start for efficient multithreaded design.

Join() seems to have been specifically designed to generate shutdown-deadlocks in GUI apps and the like.

isAlive() seems to have been specifically designed to encourage devs. to implement CPU-intensive and/or latency-ridden polling loops. I assume that it can generate false positives, ie. the thread is gone by the time it gets to return true.

I you need to 'start Thread-2 only after Thread-1 has completed', then either thread-2 is redundant, (in which case just call the function for 'thread-2' at the end of the function for thread-1), or you can simply start thread-2 at the end of the function for thread-1. No signaling/polling complication required at all.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • 1
    Not quite. I have a monitor thread that on demand polls current state data (for the internal process in the thread) from all active threads and it uses IsAlive to drop the reference to threads that are no longer running. The alternative would be to have all threads use events to always report back state but since we do not need the state all the time that probably causes more overhead. And Join is for shutdown where you need to wait for all active threads to close down before finishing cleanup and report back. In this case no single thread can trigger the "next thread". – David Mårtensson Apr 01 '15 at 13:58