Which methods are utilized to control the access to an object in…

2014

Which methods are utilized to control the access to an object in multi-threaded programming ?

  1. A.

    Asynchronized methods

  2. B.

    Synchronized methods

  3. C.

    Serialized methods

  4. D.

    None of the above

Attempted by 422 students.

Show answer & explanation

Correct answer: B

Answer: Synchronized methods

Explanation: Synchronized methods use an object's intrinsic lock (monitor) to ensure mutual exclusion so that only one thread can execute the synchronized code on that object at a time.

  • How it works: A synchronized instance method acquires the instance's lock; a synchronized static method acquires the Class object's lock.

  • Scoped locking: Use synchronized blocks to limit the locked region (for example, synchronized(this) or synchronized(someLock) { ... }) to reduce contention.

  • Alternatives: Explicit lock implementations (for example, ReentrantLock from java.util.concurrent.locks) provide features like tryLock, interruptible acquisition, and fairness policies.

  • Why the other terms are wrong: "Asynchronized methods" suggests asynchronous execution (not mutual exclusion); "Serialized methods" refers to object serialization, not concurrent access control; therefore those are not mechanisms for controlling access.

Key takeaway: Use synchronized methods/blocks or explicit locks to control access to shared objects in multi-threaded programs; choose the tool that fits your needs for scope and flexibility.

Explore the full course: Mppsc Assistant Professor