Help coderanch get a
new server
by contributing to the fundraiser
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic