Java MCQ: What does the ‘synchronized’ keyword do?
a) It allows only one thread to access a block of code at a time
b) It prevents deadlock
c) It synchronizes two variables
d) It ensures a method is called in order
Answer:
a) It allows only one thread to access a block of code at a time
Explanation:
The synchronized
keyword in Java is used to control access to a block of code or a method in a multithreaded environment. It ensures that only one thread can execute the synchronized block at a time, preventing race conditions and ensuring data consistency.
Understanding synchronized
is crucial for writing thread-safe Java programs that handle concurrent execution properly.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html