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

Threads - Acquiring the lock - Chapter 9 - Question 9

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Again Friends,



As it stands, the code will not compile as the code does not acquire a lock on t before calling t.wait();

What changes need to be made on this code for it to compile. In other words, how do I acquire the lock on t?

Thanks
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try
instead of
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glen Iris wrote:
As it stands, the code will not compile as the code does not acquire a lock on t before calling t.wait();



The code compiles for me just fine.

Regards,
Dan
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


As it stands, the code will not compile as the code does not acquire a lock on t before calling t.wait();



No, it will compile but result in a run-time exception.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will compile, but will throw a java.lang.IllegalMonitorStateException.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:Try
instead of



This works!
 
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:The code will compile, but will throw a java.lang.IllegalMonitorStateException.



It throws an IllegalMonitorStateException simply because thread "t" calling the wait() method doesn't own the lock with a synchronize(t){...} code block.


In reference to Kathy & Bert book on page 787: Thread Interaction (Exam Objective 4.4) 749 750 Chapter 9: Threads

Note that if the thread calling wait() does not own the lock, it will throw an
IllegalMonitorStateException. This exception is not a checked exception,

so you don't have to catch it explicitly. You should always be clear whether a thread
has the lock of an object in any given block of code.

Reread the Thread chapter and do some more examples that will help reinforce your knowledge and enhance your skills.

Hope it help...


 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch guys. i had a brain fart and just couldn't think how to 'acquire a lock'.

:-)
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have a question related to the below question. I am a new bee in Java Threading, so any help will be appreciated.

After making the below modification to the program, why the program waited for 10seconds. The thread which should wait is the one (i.e t) where we invoked wait(10000), not on main. Why main-thread got waited for 10 seconds?

 
Pankaj Chopra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the answer thanks!
https://coderanch.com/t/563403/java-programmer-SCJP/certification/Thread-Synchronized-Object-Locks
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daemon Pc wrote:Got the answer thanks!
https://coderanch.com/t/563403/java-programmer-SCJP/certification/Thread-Synchronized-Object-Locks


Hi,

Please UseOneThreadPerQuestion(<-click).

Also, please check your private message for an administrative matter.

Thanks.
 
The only thing that kept the leeches off of me was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic