| Author |
Threads - Acquiring the lock - Chapter 9 - Question 9
|
Glen Iris
Ranch Hand
Joined: Jul 13, 2011
Posts: 69
|
|
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
|
OCPJP 6
|
 |
Anayonkar Shivalkar
Ranch Hand
Joined: Dec 08, 2010
Posts: 671
|
|
Try
instead of
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
Dan Drillich
Ranch Hand
Joined: Jul 09, 2001
Posts: 1061
|
|
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
|
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
|
 |
Sebanti Sanyal
Ranch Hand
Joined: Nov 07, 2011
Posts: 58
|
|
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
Ranch Hand
Joined: Dec 08, 2010
Posts: 671
|
|
|
The code will compile, but will throw a java.lang.IllegalMonitorStateException.
|
 |
Dan Drillich
Ranch Hand
Joined: Jul 09, 2001
Posts: 1061
|
|
Anayonkar Shivalkar wrote:Try
instead of
This works!
|
 |
Tommy Delson
Ranch Hand
Joined: Apr 13, 2011
Posts: 206
|
|
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...
|
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
|
 |
Glen Iris
Ranch Hand
Joined: Jul 13, 2011
Posts: 69
|
|
Thanks a bunch guys. i had a brain fart and just couldn't think how to 'acquire a lock'.
:-)
|
 |
 |
|
|
subject: Threads - Acquiring the lock - Chapter 9 - Question 9
|
|
|