In Java programming, which keyword indicates that a method can be accessed by only one thread at a time to ensure mutual exclusion?

Difficulty: Easy

Correct Answer: Synchronized

Explanation:


Introduction / Context:
Concurrent programming in Java requires careful control of access to shared data. If multiple threads modify the same object without coordination, race conditions and inconsistent states can occur. Java provides several mechanisms for locking, including a keyword that can be applied to methods or blocks to ensure that only one thread can execute that code at a time for a given object or class. This question checks whether you know that keyword.


Given Data / Assumptions:

  • We are using standard Java language features for concurrency.
  • A method should be restricted so that only one thread enters it at any one time for a particular instance or class.
  • The options include common Java keywords and a generic none option.


Concept / Approach:
The synchronized keyword in Java is used to mark a method or block of code as requiring a monitor lock. For an instance method, the lock is associated with the current object. For a static method, the lock is associated with the class object. When a thread enters a synchronized method, it must first acquire the appropriate lock. Other threads attempting to enter any synchronized method or block guarded by the same lock are blocked until the lock is released. This provides mutual exclusion and helps protect shared data from concurrent modification.


Step-by-Step Solution:
Step 1: Recall that Java uses synchronized to implement built in monitor style locks.Step 2: Recognise that strictfp relates to floating point behaviour and new is used to create objects, not to control threading.Step 3: Option B lists Synchronized, which is the correct keyword for mutual exclusion on methods.Step 4: Option A and option C are unrelated to thread safety.Step 5: Option D, None, is incorrect because Java does provide a dedicated keyword for this purpose.


Verification / Alternative check:
In code such as public synchronized void increment() { counter++; }, the method is protected so that only one thread can execute it on a given object at once. If two threads call increment on the same instance, one waits until the other finishes. Removing synchronized allows both to run at the same time, which can lead to incorrect counts. This behaviour confirms that synchronized is the keyword controlling access by one thread at a time.


Why Other Options Are Wrong:
Option A, strictfp, ensures consistent floating point calculations across platforms and has nothing to do with threading. Option C, new, is used for object creation. Option D claims there is no such keyword, which contradicts the Java language specification.


Common Pitfalls:
Developers sometimes overuse synchronized, creating unnecessary contention and reducing performance. Others forget to synchronise related read and write operations, causing subtle bugs. As Java has evolved, higher level abstractions such as locks and concurrent collections have become available, but the synchronized keyword remains fundamental and often appears in exam questions about basic thread safety.


Final Answer:
Synchronized

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion