aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question K&B Book - question 12 from the 5-th version of scjp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question K&B Book - question 12 from the 5-th version of scjp" Watch "Thread Question K&B Book - question 12 from the 5-th version of scjp" New topic
Author

Thread Question K&B Book - question 12 from the 5-th version of scjp

Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Hello,
I will try to explain my doubt and sorry if this has been already explained before, I tried searching...
Here is the question:

public class Logger {
private StringBuilder contents = new StringBuilder();
public void log(String message) {
contents.append(System.currentTimeMillis());
contents.append(": ");
contents.append(Thread.currentThread().getName());
contents.append(message);
contents.append("\n");
}
public String getContents() { return contents.toString(); }
}

How can we ensure that instances of this class can be safely used by multiple threads?
A. This class is already thread-safe.
B. Replacing StringBuilder with StringBuffer will make this class thread-safe.
C. Synchronize the log() method only.
D. Synchronize the getContents() method only.
E. Synchronize both log() and getContents().
F. This class cannot be made thread-safe.



The answer is E - but I really have to disagree with that. I will try to explain: suppose you have 2 threads that access this class and it's methods, now suppose that this 2 threads have to access this methods in a particular order (obviously I am going to suppose that the order is log() method then getContents() method). Now if Thread1 has called log() method and it is synchronized (this is said to be the correct answer), it of course gets the lock of the instance Logger (or the "this" instance to be easier to understand), after the method is done, the lock is released. The next thing that I want to happen is Thread1 to go for the getContents() method - but that is not guaranteed to happen, since Thread2 might get it's own turn on the log() method. What am I missing here? I hope I was explicit.
Thank you,
Eugene.

Kieren Dixon
Greenhorn

Joined: Jan 30, 2009
Posts: 27
It will be possible to call the getContents() method from Thread1 while Thread2 has not completely finished writing to the log, thus giving erroneous results.

It is hard to see in such a small program but if you compile something similar to this:



You will note results such as:

1240855661734: Thread-0post this message
1240855669734

1240855661734: Thread-0post this message
1240855669734: Thread-9post this message
1240855677734


Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Thank you for the answer, but still you have missed my point. In you example there is just one method that is synchronized - the log() method. My question is different. I will try to rephrase it(for your example that is) - " If you synchronize both of your methods log() and getContents() will this guarantee that this class is Thread safe taking in consideration my assumption that I need both methods to be called one after the other without being "interrupted" (and by interrupted I mean mind his own business until this Thread is done taking both of the steps) by another Thread?"

Kieren Dixon wrote:It will be possible to call the getContents() method from Thread1 while Thread2 has not completely finished writing to the log, thus giving erroneous results.

It is hard to see in such a small program but if you compile something similar to this:



You will note results such as:

1240855661734: Thread-0post this message
1240855669734

1240855661734: Thread-0post this message
1240855669734: Thread-9post this message
1240855677734


Kieren Dixon
Greenhorn

Joined: Jan 30, 2009
Posts: 27
No it will not.
It will mean that (assuming both are synchronized) only one thread at a time can be currently executing the code in either of the methods and all the other threads must wait to obtain the 'lock' before calling either method.

However, it is possible to call two methods as one atomic operation, using a synchronized block.



I removed the synchronized keyword from line 23 and added the synchronized block for lines 13 and 14.

If you run this example you will see that the log is written and then the prntln is called. Therefore 1, then 2, then 3, etc lines will be printed for each call.
Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Exacly! Thank you very much - now it makes two of us that think that the answer provided in the book is not correct. Any one else here?


Kieren Dixon wrote:No it will not.
It will mean that (assuming both are synchronized) only one thread at a time can be currently executing the code in either of the methods and all the other threads must wait to obtain the 'lock' before calling either method.

However, it is possible to call two methods as one atomic operation, using a synchronized block.



I removed the synchronized keyword from line 23 and added the synchronized block for lines 13 and 14.

If you run this example you will see that the log is written and then the prntln is called. Therefore 1, then 2, then 3, etc lines will be printed for each call.
Kieren Dixon
Greenhorn

Joined: Jan 30, 2009
Posts: 27
The question does not state that the methods need to be called sequentially and atomically, just how to safe-thread the class. So E is the correct answer.


Eugene Rabii
Ranch Hand

Joined: Apr 24, 2009
Posts: 30
Totally agree with you, but of course this is somewhere "subjective" answer - it of course depends on what you want to do and how. Surely I would have chosen the E answer from the exam point of view - but if you get to think in the real world where this problem may arise - then no way this could be the answer.
Nevertheless , thank you for the comments.
Kieren Dixon wrote:The question does not state that the methods need to be called sequentially and atomically, just how to safe-thread the class. So E is the correct answer.


 
Don't get me started about those stupid light bulbs.
 
subject: Thread Question K&B Book - question 12 from the 5-th version of scjp