In a case where you are working with a multi-threaded application where multiple threads need to write to a single log file, how would you ensure that the writes to the file are thread-safe and that the data is not corrupted?

  • a. Use a single FileWriter instance shared among threads.
  • b. Implement synchronization using synchronized blocks or methods.
  • c. Create a separate log file for each thread.
  • d. Use a ThreadLocal variable for each thread's log writer.
To ensure thread-safety when multiple threads write to a single log file, you should implement synchronization using synchronized blocks or methods. This prevents concurrent writes from interfering with each other. Using a shared FileWriter instance (Option a) without synchronization may lead to data corruption. The other options do not provide effective thread-safety.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *