• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

join() mock exam question

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic