Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a synchronized method with different objects

 
Craig James
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Craig James
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the explanation Jeff. It helps a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic