File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a little clarification again about wait() notify() 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 "a little clarification again about wait() notify()" Watch "a little clarification again about wait() notify()" New topic

a little clarification again about wait() notify()

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
I have a question about my previous post regarding wait() notify()

I understood the explanation but for the exam point of view , what are we going to do if such type of question comes in paper. I mean if a thread does not do notify and the other thread calls wait on that thread object ( as i have posted earlier), so are we going to acknowledge the implementation detail fact about Thread ( according to which it calls notifyall implicitly once it completes). If we acknowledge this fact then the behavior of such a the code if asked in exam would not be a thread waiting forever and git simply gives the output. On the other hand if we do not acknowledge it then then we should say that the thread will wait for ever as there is no notify. Please clarify what to do in exam ?? I am confused because Henry wrote earlier that:

This is an implementation detail, and can't be dependent to behave this way with all JVMs.


So what to do in exam??

SCJP 6.0 (86%) SCWCD 5 (94%)
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

His answer was pretty clear. It is not part of a spec so other threads will wait forever. You cannot rely on a specific JVM / OS to give an answer to that question

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

As Deepak said, that the answer would be that the wait call will wait forever in the second code (from exam point of view). But the specific examples that you gave in that other topic, both the codes might keep waiting forever.

In this code, the run method might get the lock over ThreadB object before the main method, so the call to wait in the main method might never return. Same is with the second code. So for the first code, the output is unpredictable (output can be 45 or the program could wait forever) and for the second code, the output is that the program will wait forever...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: a little clarification again about wait() notify()
It's not a secret anymore!