Java MCQ: Which of the following is a feature of the ‘synchronized’ keyword in Java?
Answer:
Explanation:
The synchronized
keyword in Java is used to control access to a resource in a multithreaded environment. It ensures that only one thread at a time can access a block of code or a method, preventing race conditions and ensuring data consistency.
When a method or a block of code is marked as synchronized
, the thread that holds the monitor for that object (or class, in the case of static methods) is the only one allowed to execute the synchronized code. Other threads attempting to execute the same code will be blocked until the monitor is released.
Understanding the synchronized
keyword is crucial for managing concurrency and ensuring thread safety in Java programs.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html