Multi Threading
© examsiri.com
Question : 39 of 44
Marks:
+1,
-0
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:
How can we ensure that instances of this class can be safely used by multiple threads?
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?
Go to Question: