Which of the following is a feature of the ‘synchronized’ keyword in Java?

Java MCQ: Which of the following is a feature of the ‘synchronized’ keyword in Java?

a) It allows multiple threads to access a resource simultaneously
b) It prevents multiple threads from accessing a resource simultaneously
c) It is used to initialize a thread
d) It is used to terminate a thread

Answer:

b) It prevents multiple threads from accessing a resource simultaneously

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

Leave a Comment

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

Scroll to Top