File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception thrown on Notify

 
Dev Looper
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply to put:
The right way to use wait()/notify() mechanism is to
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic