Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads - Acquiring the lock - Chapter 9 - Question 9

 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
instead of
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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
 
Sebanti Sanyal
Ranch Hand
Posts: 58
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Try
instead of


This works!
 
Tommy Delson
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch guys. i had a brain fart and just couldn't think how to 'acquire a lock'.

:-)
 
Pankaj Chopra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

Please UseOneThreadPerQuestion(<-click).

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

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic