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 wait() - notify() question 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 "wait() - notify() question" Watch "wait() - notify() question" New topic

wait() - notify() question

Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Hi to all.
I can�t undestand the following thing.
Here is a code

Lets consider the following situation: after b.start() the thread b is choosen and it executes till the end. After it thread main will be choosen and will start to executes its synchronized blok, and will became wating. But, notify() method has already executed and therefore our program will become buzz.
Is it possible?

SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
C. Nimo
Ranch Hand

Joined: Mar 23, 2004
Posts: 82
Not sure what you ment about the buzz thing..
However - Your program will run well.
When the main thread enters the synchronized block, it has the
lock for b.
It then tries to wait on b - but there's no need for this wait,
since main thread already owns the lock.
If you were to add a SOP after the wait() command, you'll see it happen.
(also - add a ; after the notify() - it might help compile)

Amit Ghai
Ranch Hand

Joined: Mar 24, 2004
Posts: 35
This is not the case.Before i start explaining remember "A single thread may hold a lock more than once."
Here is what is actually happening.
When you call b.start() although you have started the new thread b, main is still running and it enters the synchronized block and it aquires the lock first, which causes your run function to hold its activity. Hence it doesnt enter the synch block in the run function. Then the b.wait() is encountered. At this the synch block leaves the monitor and b again takes the monitor this time for the run function which was suspended. it performs all the activity then it sends notify. we then return to statement at b.wait() it recieves the notification and becomes active. it has nothing to perform here ( as no function except run). Simultaneously the main thread is also working. both the threads terminate. The order might differ.
Hope you understand.
In order to achieve what you want i.e to hang the application you must make the main thread to sleep for a short duration before it enters the synch block in main. Then the output will be the one that you desire. something like this.
class ThreadA
public static void main(String[] args) throws InterruptedException
ThreadB b = new ThreadB();


catch(InterruptedException ie)


class ThreadB extends Thread
int total;
public void run()
for(int i = 0; i< 100; i++)
total += 1;

Reasonable people adapt themselves to the world. Unreasonable people attempt to adapt the world to themselves. All progress, therefore, depends on unreasonable people.
Satya Kondur

Joined: Mar 19, 2004
Posts: 8
JL Spec says
The notify method should be called for an object only
when the current thread has already locked the object�s lock.
If the wait set for the object is not empty, then some arbitrarily
chosen thread is removed from the wait set and reenabled for thread
So for the program to hang the wait set for the object should be empty
at the time when notify is called.
I agree. Here's the link:
subject: wait() - notify() question
It's not a secret anymore!