Forums Register Login

wait() - notify() question

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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();
}
}
}
+Pie Number of slices to send: Send
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.
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 649 times.
Similar Threads
notify(); doesn't work?
K&B book example
wait and notify problem from K&B.
problem with wait()- notify()
thread doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:35:30.