Java OCA OCP Practice Question 1285

Question

How do you prevent shared data from being corrupted in a multithreaded environment?

  • A. Mark all variables as synchronized.
  • B. Mark all variables as volatile.
  • C. Use only static variables.
  • D. Access the variables only via synchronized methods.


D.

Note

Variables may not be synchronized.

Making the variables volatile or static doesn't address the problem.

Shared data in a multithreaded environment should be protected by ensuring that all access to the data is via synchronized methods.

All methods should synchronize on the same lock.




PreviousNext

Related