Confussion regarding wait() method of Object
Hi,
As the suspend and resume method are deprecated
i was trying to simulate this behavior using wait() and notify() mechanism.
For this purpose i have written an application which creates a thread in which there is nothing but some
printf's. Now when i call myThread.wait() from the main
thread it gives me IllegalMonitorStateException exception
This i suppose is due to not calling wait or notify from
a synchronized method but i have called these methods
in synchronized blocks.
I also dont understand why these methods are to be called in synchronized blocks and why these methods are part of Object class and not of Thread class.
Hi,
I am also trying this using a swing application in which
there are button for pausing and resuming a thread
(which appends a sting in a JTextArea ) but this also
doesnt seems to work the thread is not going in the wait
state at all.
I know i might be going wrong with some basic things but i am not able to get it.
Regards.....
They're part of the Object class because sometimes you need notification on objects, not just threads, and you don't want to create a thread just so you can do a lock on any old object.
The methods need to be called from synchronized blocks partially because of a style point, which is somehow related to one of the solutions to the dining philosophers problem, the one where the philosopher must grab the bowl before being allowed to touch the forks (if you have no idea why I'm mentioning dining philosophers, do a search for it in a search engine.)
You, as a philosopher, enter a synchronized block to grab the bowl, and then call wait if one of the forks isn't available. The other philosopher will call notify when they're finished with the fork. If you don't set up this example by grabbing the bowl first, it's possible you'll enter deadlock (something which you always try to avoid with multi-threaded programming.)
Hi,But i thought the implementaion of Synchronized itself is done as a monitor which is infact a soultion for Dining philosopher problem.Any way my original question still remains unanswererd.
You have to determine a lock to use for the wait and notify calls. This can be any object. When you want one thread to wait for another, synchronize on the object and call wait while the flag to delay is true. When the other thread wants to tell the first thread to proceed, it should clear the flag and call notifyAll():
Waiting thread:
synchronized (syncObj) {
delay = true;
while (delay)
syncObj.wait();
}
Notifying thread:
synchronized (syncObj) {
delay = false;
syncObj.notifyAll();
}
Why synchronize on the thread object itself?
class Sleeper {
object monitor = new Object();
boolean zzz;
public sleep () {
synchronized (monitor) {
zzz = true;
while (zzz) monitor.wait();
}
}
public wakeUp () {
synchronized (monitor) {
zzz = false;
monitor.notifyAll();
}
}
}
...forgot: sleep() throws InterruptedException {
wait() and notify() can only be called from synchronized blocks. See a tutorial on Threads
I believe we already said that one. :-p
I have some code that may do what you want, but it requires that you subclass a class and call checkStop / checkSuspend / checkAndSuspend regularly in run().
E-mail me at software@NDSoft.net and I may be able to arrange for you to use it for free in a non-commercial application. I'm not sure what the companies stance is on providing code for use in commercial apps.
You simply need to synchronize over the object you are calling wait() or notify() on.
> Hi,
> But i thought the implementaion of Synchronized itself
>
> is done as a monitor which is infact a soultion for
> Dining philosopher problem.
> Any way my original question still remains
> unanswererd.
Its a feature. it does not need to be synchronized but it enhances its usability. you can get the same functionality of wait and notify without special synchronization. but you get more with the synch.