wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Using a synchronized method with different objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Using a synchronized method with different objects" Watch "Using a synchronized method with different objects" New topic
Author

Using a synchronized method with different objects

Craig James
Greenhorn

Joined: Sep 01, 2008
Posts: 2
I have a question concerning the use of a synchronized method by two different objects. I have tried searching the forums for a similar question, but I wasn't able to find anything. If this question has already been answered, then a simple link to that thread would be great. Thanks in advance for any help.

The code examples come from the Sun Certified Programmer for Java 6 Study Guide by Kathy Sierra and Bert Bates.

In the chapter 9 Self Test, there are two questions that have me confused. Question 17 (pg. 785) is:

Given:



And given these two fragments:



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.

And now question 15 (pg. 783) from the same Self Test:

Given:



And given these two fragments:


When fragment I or fragment II is inserted at line 5, which are true? (Choose all that apply.)
A. An exception is thrown at runtime
B. With fragment I, compilation fails
C. With fragment II, compilation fails
D. With fragment I, the output could be yo dude dude yo
E. With fragment I, the output could be dude dude yo yo
F. With fragment II, the output could be yo dude dude yo

Answer:

F is correct. With fragment I, the chat method is synchronized, so the two threads can't swap back and forth. With either fragment, the first output must be yo.

My question is this: Based on the answer to question 17 (that there are two different objects, therefore they can access the synchronized method at the same time), shouldn't D also be a correct answer for question 15?

Thanks once again for any help.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

I question 17, the main method specifically generates two instances of the class with the synchronized method. In question 15 it creates just one instance of the class with the synchronized method and feeds it to two threads.

Do you understand why that makes a difference?


Steve
Craig James
Greenhorn

Joined: Sep 01, 2008
Posts: 2
Originally posted by Steve Luke:
I question 17, the main method specifically generates two instances of the class with the synchronized method. In question 15 it creates just one instance of the class with the synchronized method and feeds it to two threads.

Do you understand why that makes a difference?


Thanks for your reply, Steve. I do understand the difference. I figured out the answer to my question about a minute after my first post, and was writing code to test it out as you posted your reply.

Thanks once again.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136

Why Dudes Instance variable should be static? if i remove static modifier I get NullPointerException. Can any one please explain what does it mean to have Static Instance variable in DudesChat class.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Janki Shah wrote:
Why Dudes Instance variable should be static? if i remove static modifier I get NullPointerException.


If it's non-static, then each instance of DudesChat has its own "d" variable. Then the "d" that's initialized in go() only applies to that DudesChat instance. The two new DudesChat objects created each have their own "d" and they are never set, so they retain their default null value. If "d" is static, then there's a single "d" for the DudesChat class overall, and all the instances see the same one.

It's really awful design to create one DudesChat object just so that one can create two other ones.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Thank you for the explanation Jeff. It helps a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a synchronized method with different objects
 
Similar Threads
Thread synchronization problem K&B, SCJP 6 - chapter 9, question 15
about Thread(ch09 q15)
Question 15 chapter 9 of k&B
Thread self test SCJP6
Non synchronized method