Java
Programming and Technical
Programming
class MyLogger
{
private StringBuilder logger = new StringBuilder();
public void log(String message, String user)
{
logger.append(message);
logger.append(user);
}
}
The programmer must guarantee that a single MyLogger object works properly for a multi threaded system. How must this code be changed to be thread-safe?
A. Replace StringBuilder with StringBuffer
B. Replace StringBuilder with just a String object and use the string concatenation (+=) within the log method.
C. No change is necessary, the current MyLogger code is already thread-safe.
D. Synchronize the log method
Read Solution (Total 1)
-
- (D) Synchronized keyword ensures that only one thread can access the method at a time.
- 1 year agoHelpfull: Yes(0) No(0)
Java Other Question