aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads - Acquiring the lock - Chapter 9 - Question 9 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads - Acquiring the lock - Chapter 9 - Question 9" Watch "Threads - Acquiring the lock - Chapter 9 - Question 9" New topic
Author

Threads - Acquiring the lock - Chapter 9 - Question 9

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

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, OCMJD
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Try
instead of


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
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
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

The code will compile, but will throw a java.lang.IllegalMonitorStateException.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Anayonkar Shivalkar wrote:Try
instead of


This works!
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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...



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: 164

Thanks a bunch guys. i had a brain fart and just couldn't think how to 'acquire a lock'.

:-)
Pankaj Chopra
Greenhorn

Joined: Jan 03, 2014
Posts: 2
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

Joined: Jan 03, 2014
Posts: 2
Got the answer thanks!
http://www.coderanch.com/t/563403/java-programmer-SCJP/certification/Thread-Synchronized-Object-Locks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5


Hi,

Please UseOneThreadPerQuestion(<-click).

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

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads - Acquiring the lock - Chapter 9 - Question 9