aspose file tools*
The moose likes Threads and Synchronization and the fly likes Exception thrown on Notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Exception thrown on Notify" Watch "Exception thrown on Notify" New topic
Author

Exception thrown on Notify

Dev Looper
Greenhorn

Joined: Oct 21, 2012
Posts: 2


as per my understanding, this is how the control flows or should flow(correct me if wrong) :

1. non-daemon thread invokes Main()
2. checkEquality() API is called.
3. threads t1, t2 created on the same object - obj.
4. t1 invokes start(), goes into synchronized block of run() method...is interrupted by wait() call...
5. t2 invokes start(), goes into synchronized block of run() ... (only after that monitor lock is relinquished by t1.wait())..completes the execution and notify thread t1.
6. t1 completes it's execution of run().

but i am getting the above mentioned output. I am aware that i'm trying to do wait()/notify() on object whose monitor is not acquired by the calling thread. But, i am not able to find the problematic code portion.

Thanks in adv...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Dev Looper wrote:

as per my understanding, this is how the control flows or should flow(correct me if wrong) :

1. non-daemon thread invokes Main()
2. checkEquality() API is called.
3. threads t1, t2 created on the same object - obj.
4. t1 invokes start(), goes into synchronized block of run() method...is interrupted by wait() call...


No. The main thread calls start on t1. Then the main thread wait()s for a signal via the t1 Object. Calling t1.wait() does not cause the thread t1 to wait. It is impossible to make another thread to wait. Instead, t1.wait() causes the current thread to wait() which can only be stopped if another thread calls t1.notify() or t1.notifyAll(). But the thread calling t1.wait() must first hold the lock associated with t1 before calling t1.wait(). The main thread does not have that lock and so it can not call t1.wait() and you get the exception.

5. t2 invokes start(), goes into synchronized block of run() ... (only after that monitor lock is relinquished by t1.wait())..completes the execution and notify thread t1.

No, the main thread terminates when t1.wait() causes the exception, so t2.start() is never called.
6. t1 completes it's execution of run().

but i am getting the above mentioned output. I am aware that i'm trying to do wait()/notify() on object whose monitor is not acquired by the calling thread. But, i am not able to find the problematic code portion.

The problematic code is the code which calls the wait() method on an object whose lock the current thread doesn't have. And that is t1.wait() (and would happen again at t2.notify() because you can't notify on an object whose lock you don't hold either).


Steve
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Simply to put:
The right way to use wait()/notify() mechanism is to


Warm Regards,
Rakesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception thrown on Notify