Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait() - notify() question

 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
C. Nimo
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
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)

Nimo.
 
Amit Ghai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
b.start();
Thread.sleep(1000);
synchronized(b)
{

try
{

b.wait();}
catch(InterruptedException ie)
{

}

}
}
}
class ThreadB extends Thread
{
int total;
public void run()
{
synchronized(this)
{
for(int i = 0; i< 100; i++)
{
total += 1;
}
notify();
}
}
}
 
Satya Kondur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
scheduling.
So for the program to hang the wait set for the object should be empty
at the time when notify is called.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic