• 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

Thread Locks

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to figure out what object has the lock on the thread when "notify()" is called in this code. Ideas?

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am not very sure, but I think wait() and notify() should be called from a synchronized context only... In this code, it isn't the case...

Thanks
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly, there's no lock in the main method and you get the java.lang.IllegalMonitorStateException.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

For the code that you posted :

The answer is : No thread holds the lock! since at line 12 the code attempts to execute notify() on object a1 without acquiring its lock. Result will be IllegalMonitorStateException

Now, if we fix this code to make it work, it chages to :


In this case the code works and the answer to the same question for this code is :
At when notify is called on object `a1` ( line 24 ), "The main() thread has acquired lock on monitor of object a1".

Note : In case you remove the delay incurred by Thread.sleep(100) from line 24, the thread executed by run() method will never come out of wait because notify is called even before wait is executed in line 6

Cheers,
Amit
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In case you remove the delay incurred by Thread.sleep(100) from line 24, the thread executed by run() method will never come out of wait because notify is called even before wait is executed in line 6



I do not understand this statement. How are you saying that notify is called even before wait is executed? What is the order of execution of start(), wait() and notify()?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

quote: In case you remove the delay incurred by Thread.sleep(100) from line 24, the thread executed by run() method will never come out of wait because notify is called even before wait is executed in line 6



The Quote posted meant to say that ,once a1.start() is invoked it doesnt start executing run method . a1.start() gets scheduled for executing at a later stage . so a1.notify() is executed even before wait() of run method is called
which will make the program wait forever
 
Sid Robin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

quote: In case you remove the delay incurred by Thread.sleep(100) from line 24, the thread executed by run() method will never come out of wait because notify is called even before wait is executed in line 6



The Quote posted meant to say that ,once a1.start() is invoked it doesnt start executing run method . a1.start() gets scheduled for executing at a later stage . so a1.notify() is executed even before wait() of run method is called
which is of no use
 
Tim Holmes
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone...that helps me to understand a lot better!
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
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