aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes [Thread] Synchronized - Object Locks 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 "[Thread] Synchronized - Object Locks" Watch "[Thread] Synchronized - Object Locks" New topic
Author

[Thread] Synchronized - Object Locks

Justin Cruise
Greenhorn

Joined: Jan 03, 2012
Posts: 2
I'm preparing for the Programmer Exam (Java 6) and I have some questions regarding threads. By the way, I'm using the book by Kathy Sierra and Bert Bates.

So in self-test of the book for Chapter 9 (p. 777), the question is:



What is the result of this code?
A. It prints X and exits
B. It prints X and never exits
C. It prints XY and exits almost immediately
D. It prints XY with a 10-second delay between X and Y
E. It prints XY with a 10000-second delay between X and Y
F. The code does not compile
G. An exception is thrown at runtime

Answer:
G is correct. The code does not acquire a lock on t before calling t.wait(), so it throws an
IllegalMonitorStateException. The method is synchronized, but it's not synchronized
on t so the exception will be thrown. If the wait were placed inside a synchronized(t)
block, then the answer would have been D.

Ok so I get the part that G is correct. However, I'm not sure how would D be the correct answer if the wait is placed inside a synchronized(t) block. Can someone please elaborate? My understanding is that t will be a new thread but actually does nothing because run() method is never overridden. So, only the main thread goes through the code including the synchronized block. Why will it wait for 10 seconds to print "Y" when wait is actually invoked on thread t?

I'm assuming that the code will look like this when wait is placed in a synchronized block:



Thanks!
-Justin-
mukhan myrzakulov
Greenhorn

Joined: Jan 16, 2008
Posts: 13
Justin Cruise wrote:I'm preparing for the Programmer Exam (Java 6) and I have some questions regarding threads. By the way, I'm using the book by Kathy Sierra and Bert Bates.

So in self-test of the book for Chapter 9 (p. 777), the question is:



What is the result of this code?
A. It prints X and exits
B. It prints X and never exits
C. It prints XY and exits almost immediately
D. It prints XY with a 10-second delay between X and Y
E. It prints XY with a 10000-second delay between X and Y
F. The code does not compile
G. An exception is thrown at runtime

Answer:
G is correct. The code does not acquire a lock on t before calling t.wait(), so it throws an
IllegalMonitorStateException. The method is synchronized, but it's not synchronized
on t so the exception will be thrown. If the wait were placed inside a synchronized(t)
block, then the answer would have been D.

Ok so I get the part that G is correct. However, I'm not sure how would D be the correct answer if the wait is placed inside a synchronized(t) block. Can someone please elaborate? My understanding is that t will be a new thread but actually does nothing because run() method is never overridden. So, only the main thread goes through the code including the synchronized block. Why will it wait for 10 seconds to print "Y" when wait is actually invoked on thread t?

I'm assuming that the code will look like this when wait is placed in a synchronized block:



Thanks!
-Justin-

From the Java API doc:
wait(long timeout)
Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
So the 'main' thread will wait on an object referenced by 't' and will get out of wait after 10 secs elapsed.
Sebastien French
Greenhorn

Joined: Jan 04, 2012
Posts: 26

Hello,





I'm wandering, you can have the keyword "synchronized" in method signature and in method body ?
Or maybe it is possible because we synchronize on different stuff ?
mukhan myrzakulov
Greenhorn

Joined: Jan 16, 2008
Posts: 13
Sebastien Minne wrote:Hello,





I'm wandering, you can have the keyword "synchronized" in method signature and in method body ?
Or maybe it is possible because we synchronize on different stuff ?

If you put synchronized on a method, it will use 'this' (object itself) to lock .
in this example its using different object to aquire the lock.
Sebastien French
Greenhorn

Joined: Jan 04, 2012
Posts: 26

Ok that's what I was thinking.
thanks for your reply
Justin Cruise
Greenhorn

Joined: Jan 03, 2012
Posts: 2
Oh I get it. Thanks!

However, I have another questions about threads:

Given:



And given these two fragments:
I. synchronized void move(long id) {
II. void move(long id) {

When either fragment I or fragment II is inserted at line 7, which are true? (Choose all that apply.)
A. Compilation fails
B. With fragment I, an exception is thrown
C. With fragment I, the output could be 4 2 4 2
D. With fragment I, the output could be 4 4 2 3
E. With fragment II, the output could be 2 4 2 4

Answer:
✓ C and E are correct. E should be obvious. C is correct because even though move() is
synchronized, it's being invoked on two different objects.
x A, B, and D are incorrect based on the above. (Objective 4.3)

I understand that C and E are correct. However, I just wanted to verify if output could also be 4 4 2 2 or 2 2 4 4 or 4 2 2 4 or 2 4 4 2?

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Thread] Synchronized - Object Locks