Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join() mock exam question

 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From K&B mock exams

Assuming that an interrupted exception has not been thrown and that "aLiveThread" is a runnable thread, which three guarantee that a thread will leave the running state?

a) yield()
b) wait()
c) notify()
d) notifyAll()
e) sleep(1000)
f) aLiveThread.join()
g) Thread.killThread()


The correct answers are b e and f.
I dont understand why f is correct. is it not the thread that is calling that method that will leave the running state? In this case main will leave the running state and wait for aLiveThread to finish??
thanks
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call wait() the running thread releases all locks and waits to be notified by another thread that a condition just happened. In this case, the running thread will block until the condition happens, and then running thread will temporarily abandon the running state.

In the second case, join() will assure that the currently running thread will block until the thread whose join method is called has finished its execution, in other words, when the run() method of the joined thread completes (whether normally o becauase of an exception).

In this case, the running thread will temporarily abandon the running state until the joined thread finishes.
 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh the question reads "that A thread leaves the running state". I misread that - I thought it meant aLiveThread leaves the running state. In which case f wouldn't be correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic