aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Small Doubt on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Small Doubt on Threads" Watch "Small Doubt on Threads" New topic
Author

Small Doubt on Threads

Vepa Sritej
Ranch Hand

Joined: Jul 31, 2006
Posts: 42
Pls help me with this small doubt:

When notify() is called on an object will the thread waiting on the object comes to Runnable state immediately or will it come to runnable when the lock is released by the currently holding thread.

Pls help Thanks in advance


tej<br /> <br />SCJP 1.5 --75%<br />SCWCD --89%<br />SCBCD/Web Services!! Yet to Decide
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Originally posted by Vepa Sritej:
Pls help me with this small doubt:

When notify() is called on an object will the thread waiting on the object comes to Runnable state immediately or will it come to runnable when the lock is released by the currently holding thread.

Pls help Thanks in advance


The thread that got the notification doesn't immediately run -- it still has to reacquire the lock before it can return from the wait() method.

Of course, other issues (like priority) may also prevent it from running too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Vepa,

I think the second is correct, the notified thread has to wait for the lock being released.
And notify() will not release the key, the thread calling notify has to leave the sync block first. If he's the one having that lock.

Api says about this:
java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#notify()
Second paragraph:
"The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object."


Yours,
Bu.


all events occur in real time
Vepa Sritej
Ranch Hand

Joined: Jul 31, 2006
Posts: 42
Thanks Burkhard,

thats what i thought but in one of the mock exams ive encountered a question stating " when will the thread enters the runnable state and the answer given is "when notify() is called". Im doubtful if this is an absolute answer.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
It is assumed that you call notify() just before the end of your synchronized block. But again, it's not a canonical answer as explained above.


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Small Doubt on Threads