This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: Chapter 9 (Threads), Q12. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B SCJP 5: Chapter 9 (Threads), Q12." Watch "Doubt in K&B SCJP 5: Chapter 9 (Threads), Q12." New topic

Doubt in K&B SCJP 5: Chapter 9 (Threads), Q12.

Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30
Hi folks,

can I get your views on this.

"Given the scenario: This class is intended to allow users to write a series of messages, so that each message is identified with a timestamp and the name of the thread that wrote the message

public class Logger
private StringBuilder contents = new StringBuilder();

public void log(String message)
contents.append(": ");

public String getContents()
return contents.toString();

How can we ensure that instances of this class can be safely used by multiple threads?"

Apparently, the right answer is "Synchronize both log() and getContents()".

In my opinion, this would still not be enough, as one thread could still be pulling out other thread's message, when it called the getContent() method. Am I wrong?

Mamadou Tour�
ranch hand
Member # 162107


Once getContents() is synchronized, the thread that acquires the lock on that method, is the same that acquires the lock on log() method. Therefore, any other thread can't enter both methods. Remember this, when a thread acquires a lock, it acquires the lock on all the synchronized methods

Hope this will help

[ May 19, 2008: Message edited by: Mamadou Tour� ]


Eduardo Mendes
Member # 170908
Thanks Mamadou, but could it not happen that the thread looses the lock to another thread, between writing the message and eventually invoking the getContent()?

e.g. in UtilityClass
Logger logger = new Logger();

in MyThread..
// Get logger instance
Logger myLogger = utilityClass.getLogger();

// doSomething else, release lock on 'logger' instance
// other thread gets the Logger from utility class, and invokes the log() method on it

// MyThread now calls getContent(), getting the other thread's message



Mamadou Tour�

Once we enter the first synchronized non-static method, we acquire the lock associated the current instance, therefore all the synchronized method are locked an no other thread can acquire the lock.


Eduardo Mendes

I understand that. But what if the thread calls wait() on an object it depends on, between calling the first and the second method? Will it not give up on its locks at that stage? That would then leads to the situation I described above. Please, correct me if I am wrong, thanks!

Mamadou Tour�

Off course when a wait is called, the thread releases its lock, and another thread could acquire it, but in the case given by the K&B exercise, there was no wait called


Eduardo Mendes

Sure, but the thread code was not presented at all either. And the question was "How can we ensure that innstances of this class will be safely used by multiple threads". So, my answer would be - This class, as it is, cannot be made thread-safe, unless we make the call to log() and getContents() from within a synchronized block in the thread.
Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35

I think Eduardo has a point. This is a bit like like "thread-safe" classes like the StringBuffer which may not provide thread safety unless implemented properly.
[ May 19, 2008: Message edited by: Royston Monteiro ]

Sun Certified Java Programmer 1.5,
Sun Certified Web Component Developer 5.
"No pain. No Gain"
Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30
Royston, that's the perfect example. I would really like to get some feedback from the authors on this, and some assurance that, if the answer provided to this question is indeed wrong, a similar situation will not happen during the SCJP5 exam.
Eduardo Mendes
Ranch Hand

Joined: Apr 30, 2008
Posts: 30
Any update? Maybe a little help from the forum moderators?
Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
I am bit confused from above discussion,in the end Eduardo stated that the method call should be put in a synchronized block.
1:Synchronize method body
2:Synchronizing method call

can you please tell me will both of the above ways of synchronization will have same effect on the program output.
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
There is a special hat that I need to be wearing to look into threads questions, and that hat isn't here right now, so I'm going to respond to this question at a higher level.

In general for all of the Sun Java exams, it is assumed that if the question hints at other code that you can't see, that unseen code is correct. Again, without my hat on I don't know whether that answers this question...



Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I agree. Here's the link:
subject: Doubt in K&B SCJP 5: Chapter 9 (Threads), Q12.
jQuery in Action, 3rd edition