• 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

wait() - notify() question

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
}
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic