单项选择题
public class MyLogger {
private StringBuilder logger = new StringBuuilder();
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. synchronize the log method
B. replace StringBuilder with StringBuffer
C. No change is necessary, the current MyLogger code is already thread-safe.
D. replace StringBuilder with just a String object and use the string concatenation (+=) within the log method
相关考题
-
单项选择题
Given this method in a class: public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(<); buffer.append(this.name); buffer.append(>); return buffer.toString(); } Which is true?()
A. This code is NOT thread-safe.
B. The programmer can replace StringBuffer with StringBuilder with no other changes.
C. This code will perform well and converting the code to use StringBuilder will not enhance the performance.
D. This code will perform poorly. For better performance, the code should be rewritten: return “<“+ this.name + “>”; -
单项选择题
11. class Converter { 12. public static void main(String[] args) { 13. Integer i = args[0]; 14. int j = 12; 15. System.out.println(”It is “ + (j==i) + “that j==i.”); 16. } 17. } What is the result when the programmer attempts to compile the code and run it with the command java Converter 12?()
A. It is true that j==i.
B. It is false that j==i.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13. -
单项选择题
public class TestString 1 { public static void main(String[] args) { String str = “420”; str += 42; System.out.print(str); } } What is the output?()
A. 42
B. 420
C. 462
D. 42042
E. Compilation fails.
F. An exception is thrown at runtime.
