• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Exception thrown on Notify

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply to put:
The right way to use wait()/notify() mechanism is to
 
Dinner will be steamed monkey heads with a side of tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic