• 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

Blocked threads

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from a question in Enthuware's SCJP 6 exam suite.

It says that a thread which is blocked because it's trying to enter synchronized block will return to runnable when notify() or notifyAll() are called.

I don't understand that, I thought a blocked thread returns to runnable when the lock is released, but didn't know that notify() or notifyAll() were necessary, and I only thought those methods were effective unless the thread had issued a wait() call.

This brings another question: If a thread tries to acquire a lock but the lock is not available, does that put the thread in the blocked state immediately, and if so, how does the thread come back to runnable? Does the JVM notify the thread that the lock has been released?

Thanks in advance for your answers.

Edit: Does the SCJP 6 exam even cover blocked threads? I seem to remember from the K&B book something about that not being covered.

[ December 18, 2008: Message edited by: Ruben Soto ]
[ December 18, 2008: Message edited by: Ruben Soto ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It says that a thread which is blocked because it's trying to enter synchronized block will return to runnable when notify() or notifyAll() are called.



Well, clearly this statement is incorrect. So you should either (1) confirm that you quoted the question right, and (2) if so, file an errata.

Henry
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry. I'll file an errata with the folks of Enthuware.

Here is the exact wording of their Question, answer, and explanation:

Question:In which of the following cases a thread will definitely be alive but not be running?

Answer: The thread is trying to enter a synchronized block and the monitor is not free.

Explanation: It will be blocked till somebody calls notify()/notifyAll() .

(Additional Answers/Explanations to the Question above omitted here.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic