Thread Synchronization: synchronized Methods and Blocks Notes in Hindi & English : थ्रेड सिंक्रोनाइज़ेशन: सिंक्रोनाइज़्ड मेथड्स, मॉनिटर लॉक्स और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Thread Synchronization: synchronized Methods and Blocks (थ्रेड सिंक्रोनाइज़ेशन: सिंक्रोनाइज़्ड मेथड्स, मॉनिटर लॉक्स) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Thread synchronization coordinates concurrent access to shared mutable state so multiple threads do not produce race conditions or corrupted memory visibility. Java enforces mutual exclusion and happens-before visibility guarantees using intrinsic monitor locks on synchronized methods, static synchronized methods, and synchronized blocks.

अध्याय सार: थ्रेड सिंक्रोनाइज़ेशन साझा म्यूटेबल डेटा तक कॉनकरेंट एक्सेस को नियंत्रित करता है ताकि रेस कंडीशन और मेमोरी विजिबिलिटी त्रुटियां न हों। जावा में synchronized मेथड्स, static synchronized मेथड्स और synchronized ब्लॉक्स द्वारा इंट्रिंसिक मॉनिटर लॉक के माध्यम से म्यूचुअल एक्सक्लूजन व हैपन्स-बिफोर गारंटी प्रदान की जाती है।

Thread Synchronization: synchronized Methods and Blocks: Detailed Notes & Architecture (थ्रेड सिंक्रोनाइज़ेशन: सिंक्रोनाइज़्ड मेथड्स, मॉनिटर लॉक्स)

1. Race Conditions and Intrinsic Monitor Locks (रेस कंडीशन और इंट्रिंसिक मॉनिटर लॉक्स)

[EN] When multiple threads concurrently read-modify-write a shared counter like count++, the operation compiles into three non-atomic bytecode steps (load, increment, store), enabling threads to interleave and lose updates—a classic Race Condition. Every Java object (and every loaded Class object) possesses a single intrinsic monitor lock (mutex). Marking critical sections synchronized guarantees mutual exclusion (one thread inside at a time) plus Java Memory Model happens-before visibility so changes made before releasing a lock are guaranteed visible to the next thread acquiring that lock. Java intrinsic locks are reentrant: a thread already holding an object's lock can enter other synchronized methods on the same object without self-deadlock.

[HI] जब कई थ्रेड्स एक साथ count++ जैसे साझा वेरिएबल को बदलते हैं, तो तीन अलग-अलग बाइटकोड स्टेप्स (पढ़ना, बढ़ाना, लिखना) आपस में टकराने से अपडेट खो सकते हैं—इसे रेस कंडीशन कहते हैं। जावा के हर ऑब्जेक्ट (और Class ऑब्जेक्ट) के पास एक इंट्रिंसिक मॉनिटर लॉक होता है। synchronized उपयोग करने से म्यूचुअल एक्सक्लूजन और जावा मेमोरी मॉडल की हैपन्स-बिफोर विजिबिलिटी मिलती है। जावा के मॉनिटर लॉक रीएंट्रेंट होते हैं, अर्थात लॉक पकड़े हुए थ्रेड उसी ऑब्जेक्ट के अन्य synchronized मेथड्स में बिना डेडलॉक के प्रवेश कर सकता है।

2. synchronized Instance Methods vs Static synchronized Methods (सिंक्रोनाइज़्ड इंस्टेंस मेथड बनाम स्टैटिक सिंक्रोनाइज़्ड मेथड)

[EN] An instance synchronized method (public synchronized void deposit()) acquires the monitor lock on the specific instance (`this`), meaning two threads operating on different Account objects do not block each other, while two threads accessing the same Account instance serialize sequentially. By contrast, a static synchronized method (public static synchronized void log()) acquires the lock on the Class metadata object (`Account.class`), blocking any thread calling any static synchronized method of that class regardless of instance count. Importantly, locking on `this` and locking on `Account.class` are completely independent locks.

[HI] इंस्टेंस synchronized मेथड (जैसे public synchronized void deposit()) वर्तमान ऑब्जेक्ट इंस्टेंस (`this`) पर लॉक लगाता है, जिससे अलग-अलग ऑब्जेक्ट्स पर काम करने वाले थ्रेड एक-दूसरे को ब्लॉक नहीं करते। इसके विपरीत, static synchronized मेथड (`ClassName.class`) क्लास-लेवल ऑब्जेक्ट पर लॉक लगाता है, जो सभी इंस्टेंस के लिए साझा स्टैटिक डेटा की सुरक्षा करता है। इंस्टेंस लॉक और क्लास लॉक पूरी तरह स्वतंत्र होते हैं और एक-दूसरे को नहीं रोकते।

3. synchronized Blocks and Fine-Grained Locking (सिंक्रोनाइज़्ड ब्लॉक और फाइन-ग्रेन्ड लॉकिंग)

[EN] Synchronizing an entire method often hurts concurrency when only a few lines actually mutate shared state. A synchronized block—`synchronized(lockObject) { ... }`—restricts mutual exclusion strictly to the minimal critical section while letting surrounding validation, logging, or I/O run in parallel. Best practice uses a dedicated `private final Object lock = new Object();` rather than locking on `this` or interned String literals/boxed Integers, preventing external callers from accidentally causing lock contention or deadlocks.

[HI] पूरे मेथड को synchronized करने से प्रदर्शन घट सकता है यदि केवल कुछ ही पंक्तियों में साझा डेटा बदल रहा हो। synchronized(lockObject) ब्लॉक क्रिटिकल सेक्शन को सीमित करके कॉनकरेंसी बढ़ाता है ताकि गैर-साझा कोड समानांतर चल सके। सर्वोत्तम अभ्यास में `this` या स्ट्रिंग लिटरल्स के बजाय एक `private final Object lock = new Object();` का उपयोग किया जाता है।

4. Deadlock, volatile Keyword, and java.util.concurrent.locks (डेडलॉक, volatile कीवर्ड और आधुनिक Locks API)

[EN] Deadlock occurs when two or more threads hold locks while waiting forever for locks held by each other (e.g., Thread 1 acquires Lock A and wants Lock B; Thread 2 holds Lock B and wants Lock A), prevented by enforcing a consistent global lock acquisition order. Meanwhile, the `volatile` keyword guarantees cross-thread visibility and prevents instruction reordering for single reads/writes without locking, but does NOT make compound operations like `count++` atomic. For advanced control—non-blocking `tryLock()`, timed lock waits, interruptible acquisition, and fair queuing—Java 5 introduced `ReentrantLock` in java.util.concurrent.locks.

[HI] डेडलॉक तब होता है जब दो थ्रेड एक-दूसरे के लॉक के खुलने का अनंत इंतजार करते हैं; इससे बचने के लिए लॉक हमेशा एक निश्चित क्रम में प्राप्त करने चाहिए। वहीं `volatile` कीवर्ड वेरिएबल के मान की तुरंत विजिबिलिटी और रीऑर्डरिंग रोकता है, परंतु `count++` जैसे कंपाउंड ऑपरेशंस को एटॉमिक नहीं बनाता। `tryLock()`, टाइमआउट और फेयर लॉकिंग के लिए जावा 5 में `ReentrantLock` प्रदान किया गया है।

Syntax, Formula & Technical Example (Thread Synchronization: synchronized Methods and Blocks)

class Counter {
    private int count = 0;
    private final Object lock = new Object();
    public synchronized void incMethod() { count++; } // Locks on 'this'
    public void incBlock() {
        // Non-critical work can run concurrently here
        synchronized (lock) { count++; } // Fine-grained critical section
    }
    public static synchronized void resetGlobal() {} // Locks on Counter.class
}

Thread Synchronization: synchronized Methods and Blocks Quick Reference & Comparison Table

Synchronization MechanismLock / Scope TargetKey Concurrency Property
Instance synchronized MethodCurrent object instance (this)Blocks other threads on same instance; reentrant
Static synchronized MethodClass object (ClassName.class)Serializes access across all instances of the class
synchronized(lock) BlockSpecified explicit monitor objectMinimizes critical section length for higher throughput
volatile Field / ReentrantLockMain memory barrier / Explicit Lock APIvolatile = visibility only; ReentrantLock adds tryLock()

Thread Synchronization: synchronized Methods and Blocks Top 20 Bilingual MCQs with Answers

Q1. Which object's monitor lock is acquired by an instance-level synchronized method?

(इंस्टेंस-लेवल synchronized मेथड द्वारा किस ऑब्जेक्ट का मॉनिटर लॉक प्राप्त किया जाता है?)

(A) The current object instance (this)

(B) A global JVM-wide lock

(C) The current Thread object

(D) The Class metadata object (ClassName.class)

Correct Answer: (A)

[EN] An instance synchronized method synchronizes on `this`, locking per individual object instance.

[HI] इंस्टेंस synchronized मेथड `this` (वर्तमान ऑब्जेक्ट) पर मॉनिटर लॉक प्राप्त करता है।

Q2. Which monitor lock is acquired when a thread invokes a static synchronized method of class Bank?

(जब कोई थ्रेड Bank क्लास के static synchronized मेथड को कॉल करता है, तो कौन सा लॉक लगता है?)

(A) Thread.currentThread()

(B) this reference of the caller

(C) The first field declared in Bank

(D) Bank.class (the Class object)

Correct Answer: (D)

[EN] Static methods belong to the class rather than an instance, so they lock on the Class literal `Bank.class`.

[HI] स्टैटिक मेथड क्लास से संबंधित होता है, इसलिए वह `Bank.class` ऑब्जेक्ट पर लॉक लगाता है।

Q3. What happens if Thread A holds `obj`'s lock inside `synchronized void m1()` and calls `synchronized void m2()` on the same `obj`?

(यदि थ्रेड A के पास `obj` का लॉक `m1()` में है और वह उसी `obj` के `synchronized void m2()` को कॉल करता है, तो क्या होगा?)

(A) Throws IllegalMonitorStateException

(B) Thread A deadlocks waiting for itself

(C) Throws ConcurrentModificationException

(D) It succeeds immediately without blocking because Java intrinsic locks are reentrant

Correct Answer: (D)

[EN] Java's monitor locks track owner thread and hold count, allowing reentrant re-entry by the same thread.

[HI] जावा के मॉनिटर लॉक्स रीएंट्रेंट होते हैं, इसलिए वही थ्रेड बिना किसी डेडलॉक के दूसरे synchronized मेथड में प्रवेश कर जाता है।

Q4. If Thread 1 is executing an instance `synchronized` method on `obj`, can Thread 2 simultaneously execute a `static synchronized` method of the same class?

(यदि थ्रेड 1 किसी ऑब्जेक्ट `obj` के इंस्टेंस `synchronized` मेथड में है, तो क्या थ्रेड 2 उसी क्लास के `static synchronized` मेथड को एक साथ चला सकता है?)

(A) Yes, because `this` and `ClassName.class` are two completely separate monitor locks

(B) No, Thread 2 enters BLOCKED state

(C) Throws IllegalThreadStateException

(D) No, static synchronized overrides instance locks

Correct Answer: (A)

[EN] Instance locks (`this`) and class locks (`ClassName.class`) never interfere with each other.

[HI] हाँ, क्योंकि `this` (इंस्टेंस लॉक) और `ClassName.class` (क्लास लॉक) दो बिल्कुल अलग मॉनिटर लॉक्स हैं।

Q5. Why is marking a shared counter `volatile int count` NOT sufficient to make `count++` thread-safe?

(साझा काउंटर को `volatile int count` घोषित करना `count++` को थ्रेड-सेफ बनाने के लिए पर्याप्त क्यों नहीं है?)

(A) `volatile` ensures memory visibility, not atomicity of the read-increment-write compound operation

(B) `volatile` stores variables in ThreadLocal stack memory

(C) `volatile` locks the entire CPU pipeline

(D) `volatile` only works on boolean variables

Correct Answer: (A)

[EN] `count++` consists of three separate operations (read, add 1, write); use `synchronized` or `AtomicInteger` for atomicity.

[HI] `volatile` केवल मेमोरी विजिबिलिटी देता है, लेकिन `count++` (रीड-ऐड-राइट) की तीन चरणों वाली प्रक्रिया को एटॉमिक नहीं बनाता।

Q6. What is the primary performance advantage of a `synchronized(lock)` block over a `synchronized` method?

(`synchronized` मेथड की तुलना में `synchronized(lock)` ब्लॉक का मुख्य प्रदर्शन लाभ क्या है?)

(A) It allows multiple threads inside the block at once

(B) It automatically prevents all deadlocks

(C) It skips acquiring any JVM monitor lock

(D) It locks only the critical code lines rather than the entire method, increasing parallelism

Correct Answer: (D)

[EN] Reducing critical-section lock hold duration minimizes contention while keeping shared mutations safe.

[HI] यह पूरे मेथड के बजाय केवल महत्वपूर्ण क्रिटिकल सेक्शन को लॉक करता है, जिससे अन्य कोड समानांतर चल सकता है।

Q7. Which four-step condition scenario causes a classic Deadlock between Thread 1 and Thread 2?

(थ्रेड 1 और थ्रेड 2 के बीच क्लासिक डेडलॉक किस स्थिति में उत्पन्न होता है?)

(A) Thread 1 interrupts Thread 2 inside a synchronized block

(B) Thread 1 holds Lock A waiting for Lock B, while Thread 2 holds Lock B waiting for Lock A

(C) Thread 1 and Thread 2 both wait for Lock A using tryLock()

(D) Thread 1 sleeps while holding Lock A for 100 ms

Correct Answer: (B)

[EN] Circular wait with non-preemptible mutual exclusion causes both threads to stay BLOCKED indefinitely.

[HI] जब थ्रेड 1 लॉक A पकड़कर B का इंतजार करे और थ्रेड 2 लॉक B पकड़कर A का इंतजार करे (सर्कुलर वेट), तो डेडलॉक होता है।

Q8. Why is locking on a String literal like `synchronized("LOCK")` considered a security/concurrency anti-pattern?

(`synchronized("LOCK")` जैसे स्ट्रिंग लिटरल पर लॉक लगाना खराब डिज़ाइन क्यों माना जाता है?)

(A) String literals are interned globally in the String Pool, so unrelated classes could lock the exact same object

(B) String literals are garbage collected during execution

(C) String objects do not have an intrinsic monitor lock

(D) synchronized only accepts primitive integers

Correct Answer: (A)

[EN] Because JVM interns identical String literals across the entire application, unrelated code can accidentally contend or deadlock.

[HI] स्ट्रिंग लिटरल पूरे JVM के स्ट्रिंग पूल में साझा होते हैं, जिससे अनजान कोड भी उसी ऑब्जेक्ट पर लॉक लगाकर डेडलॉक कर सकता है।

Q9. What happens if an unhandled RuntimeException is thrown inside a `synchronized` block?

(यदि किसी `synchronized` ब्लॉक के अंदर कोई अनहैंडल्ड RuntimeException उत्पन्न हो जाए, तो मॉनिटर लॉक के साथ क्या होता है?)

(A) The JVM automatically releases the monitor lock as execution exits the synchronized block

(B) All waiting threads throw IllegalMonitorStateException

(C) The object is immediately deleted by GC

(D) The monitor lock remains locked forever

Correct Answer: (A)

[EN] Intrinsic synchronized monitors always unlock cleanly on abnormal exception exit, unlike manual ReentrantLock (which needs finally).

[HI] synchronized ब्लॉक से एक्सेप्शन के साथ बाहर निकलने पर भी JVM मॉनिटर लॉक को स्वतः अनलॉक कर देता है।

Q10. Which keyword CANNOT be applied to a constructor in Java?

(जावा में कंस्ट्रक्टर (Constructor) के साथ निम्नलिखित में से कौन सा कीवर्ड नहीं लगाया जा सकता?)

(A) synchronized

(B) public

(C) private

(D) protected

Correct Answer: (A)

[EN] Constructors cannot be declared synchronized (only a single thread constructs an instance), though synchronized blocks inside a constructor are valid.

[HI] कंस्ट्रक्टर को synchronized घोषित नहीं किया जा सकता (क्योंकि निर्माण एक ही थ्रेड करता है), हालांकि अंदर synchronized ब्लॉक लिखा जा सकता है।

Q11. Which class in `java.util.concurrent.locks` supports non-blocking `tryLock()` and fair lock ordering?

(`java.util.concurrent.locks` पैकेज की कौन सी क्लास नॉन-ब्लॉकिंग `tryLock()` और फेयर लॉकिंग की सुविधा देती है?)

(A) ReentrantLock

(B) MonitorLock

(C) SynchronizedLock

(D) ThreadLock

Correct Answer: (A)

[EN] ReentrantLock provides tryLock(), tryLock(timeout, unit), lockInterruptibly(), and an optional fair constructor.

[HI] ReentrantLock में tryLock(), टाइमआउट लॉकिंग, lockInterruptibly() और फेयरनेस पॉलिसी जैसी उन्नत सुविधाएं होती हैं।

Q12. Can an unsynchronized method on `obj` run concurrently while another thread holds `obj`'s lock in a `synchronized` method?

(जब एक थ्रेड `obj` के `synchronized` मेथड का लॉक पकड़े हुए हो, तो क्या दूसरा थ्रेड उसी `obj` का नॉन-सिंक्रोनाइज़्ड मेथड चला सकता है?)

(A) No, the entire object becomes unreadable to all threads

(B) Yes, non-synchronized methods do not acquire any monitor lock and run unimpeded

(C) Only if Thread priority is MAX_PRIORITY

(D) Only if the non-synchronized method is marked static

Correct Answer: (B)

[EN] Synchronization only coordinates threads that actually attempt to acquire the same monitor lock.

[HI] हाँ, नॉन-सिंक्रोनाइज़्ड मेथड्स मॉनिटर लॉक नहीं मांगते, इसलिए वे बिना रुकावट के समानांतर चल सकते हैं।

Q13. What is the recommended atomic lock-free alternative in `java.util.concurrent.atomic` for a shared integer counter?

(साझा इंटीजर काउंटर के लिए `java.util.concurrent.atomic` में अनुशंसित लॉक-फ्री विकल्प कौन सा है?)

(A) AtomicInteger

(B) ConcurrentInt

(C) SyncInteger

(D) VolatileInteger

Correct Answer: (A)

[EN] AtomicInteger uses hardware Compare-And-Swap (CAS) instructions via incrementAndGet() for high-throughput thread safety.

[HI] AtomicInteger हार्डवेयर CAS (Compare-And-Swap) तकनीक द्वारा बिना लॉक के थ्रेड-सेफ काउंटर ऑपरेशन प्रदान करता है।

Q14. What happens when you write `synchronized(null) { ... }` in Java?

(जावा में `synchronized(null) { ... }` लिखने पर क्या होता है?)

(A) No-op; skips synchronization silently

(B) Throws NullPointerException at runtime when entering the block

(C) Compiles and acquires a global null monitor

(D) Fail-to-compile syntax error always

Correct Answer: (B)

[EN] Attempting to enter a synchronized block on a null reference expression throws NullPointerException at runtime.

[HI] यदि synchronized ब्लॉक को दिया गया रेफरेंस रनटाइम पर null हो, तो JVM तुरंत NullPointerException फेंकता है।

Q15. Which technique reliably eliminates circular-wait deadlocks when multiple threads must acquire Locks L1 and L2?

(जब कई थ्रेड्स को लॉक L1 और L2 दोनों की आवश्यकता हो, तो सर्कुलर-वेट डेडलॉक से बचने का विश्वसनीय तरीका क्या है?)

(A) Always acquire locks in a fixed global order (e.g., always L1 before L2 in every thread)

(B) Replace synchronized blocks with Thread.sleep(10)

(C) Use Thread.yield() before acquiring each lock

(D) Increase JVM heap size with -Xmx

Correct Answer: (A)

[EN] Consistent lock ordering breaks the circular-wait Coffman condition, making deadlock mathematically impossible.

[HI] सभी थ्रेड्स में हमेशा एक ही निश्चित क्रम (जैसे पहले L1, फिर L2) में लॉक लेने से सर्कुलर-वेट की संभावना समाप्त हो जाती है।

Q16. Which of the following statements about Java Synchronization is NOT true?

(जावा सिंक्रोनाइज़ेशन के बारे में निम्नलिखित में से कौन सा कथन असत्य (NOT true) है?)

(A) Locking an entire large method needlessly increases thread contention

(B) A thread entering a `synchronized` block establishes a happens-before relationship on that monitor

(C) Both `synchronized` methods and `ReentrantLock` automatically release their locks without a `finally` block

(D) Java's intrinsic `synchronized` locks are reentrant for the owning thread

Correct Answer: (C)

[EN] Unlike `synchronized`, `ReentrantLock` does NOT auto-unlock on exit or exception; you must call `lock.unlock()` inside a `finally` block.

[HI] `synchronized` स्वतः अनलॉक हो जाता है, लेकिन `ReentrantLock` को `finally` ब्लॉक में मैन्युअल रूप से `unlock()` करना अनिवार्य होता है।

Q17. Within Thread Synchronization: synchronized Methods and Blocks, which synchronization mechanism directly maps to: "Current object instance (this)"?

(Thread Synchronization: synchronized Methods and Blocks में कौन सा Synchronization Mechanism सीधे "Current object instance (this)" से संबंधित है?)

(A) synchronized(lock) Block

(B) volatile Field / ReentrantLock

(C) Static synchronized Method

(D) Instance synchronized Method

Correct Answer: (D)

[EN] In Thread Synchronization: synchronized Methods and Blocks, 'Instance synchronized Method' handles 'Current object instance (this)' (Blocks other threads on same instance; reentrant).

[HI] Thread Synchronization: synchronized Methods and Blocks में 'Instance synchronized Method' का कार्य/विवरण 'Current object instance (this)' (Blocks other threads on same instance; reentrant) है।

Q18. Which option accurately describes 'Static synchronized Method' in Thread Synchronization: synchronized Methods and Blocks?

(Thread Synchronization: synchronized Methods and Blocks के संदर्भ में 'Static synchronized Method' की सही विशेषता कौन सी है?)

(A) Main memory barrier / Explicit Lock API

(B) Specified explicit monitor object

(C) Class object (ClassName.class)

(D) Current object instance (this)

Correct Answer: (C)

[EN] 'Static synchronized Method' provides 'Class object (ClassName.class)' — key point: Serializes access across all instances of the class.

[HI] 'Static synchronized Method' का मुख्य विवरण 'Class object (ClassName.class)' है (Serializes access across all instances of the class)।

Q19. In Thread Synchronization: synchronized Methods and Blocks, identify the correct parameter for the specification: "Specified explicit monitor object".

(Thread Synchronization: synchronized Methods and Blocks में "Specified explicit monitor object" विवरण किस विकल्प के लिए सही है?)

(A) Instance synchronized Method

(B) volatile Field / ReentrantLock

(C) synchronized(lock) Block

(D) Static synchronized Method

Correct Answer: (C)

[EN] 'synchronized(lock) Block' corresponds to 'Specified explicit monitor object' (Minimizes critical section length for higher throughput).

[HI] 'synchronized(lock) Block' का संबंध 'Specified explicit monitor object' (Minimizes critical section length for higher throughput) से है।

Q20. Considering Thread Synchronization: synchronized Methods and Blocks, what key behavior defines 'volatile Field / ReentrantLock'?

(Thread Synchronization: synchronized Methods and Blocks में 'volatile Field / ReentrantLock' की मुख्य भूमिका या मान क्या है?)

(A) Specified explicit monitor object

(B) Current object instance (this)

(C) Class object (ClassName.class)

(D) Main memory barrier / Explicit Lock API

Correct Answer: (D)

[EN] 'volatile Field / ReentrantLock' represents 'Main memory barrier / Explicit Lock API' (volatile = visibility only; ReentrantLock adds tryLock()).

[HI] 'volatile Field / ReentrantLock' का अर्थ/मान 'Main memory barrier / Explicit Lock API' है (volatile = visibility only; ReentrantLock adds tryLock())।

Thread Synchronization: synchronized Methods and Blocks — Exam FAQs

Q1: Why is there no synchronized keyword allowed for variables in Java?

जावा में वेरिएबल्स (Variables) के साथ synchronized कीवर्ड की अनुमति क्यों नहीं है?

[EN] Synchronization applies to critical execution sections (methods or blocks) guarded by an object's monitor lock; for variable visibility alone, Java provides `volatile` and `java.util.concurrent.atomic` classes.

[HI] सिंक्रोनाइज़ेशन कोड ब्लॉक्स या मेथड्स के एक्सेस को नियंत्रित करने के लिए होता है; वेरिएबल्स की विजिबिलिटी के लिए `volatile` या Atomic क्लासेस का उपयोग होता है।

Q2: When should I choose ReentrantLock over synchronized?

synchronized के बजाय ReentrantLock का चुनाव कब करना चाहिए?

[EN] Use `synchronized` by default for simplicity and automatic unlocking; choose `ReentrantLock` when you need non-blocking `tryLock()`, timed lock acquisition, interruptible lock waiting, fairness ordering, or multiple `Condition` wait-sets.

[HI] सरलता के लिए `synchronized` चुनें, लेकिन जब `tryLock()`, टाइमआउट, फेयर लॉकिंग या एक से अधिक Condition की जरूरत हो, तब `ReentrantLock` अपनाएं।

Q3: Does synchronized slow down single-threaded code drastically?

क्या synchronized सिंगल-थ्रेडेड कोड को बहुत धीमा कर देता है?

[EN] Uncontended monitor entry is highly optimized in modern HotSpot JVMs (via thin locks / fast-path CAS), though heavy multi-threaded contention on a coarse lock still creates blocking overhead.

[HI] आधुनिक JVM में बिना कंटेंशन (Uncontended) वाले लॉक बहुत ऑप्टिमाइज़्ड होते हैं, लेकिन कई थ्रेड्स के एक ही लॉक पर टकराने से ओवरहेड बढ़ता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)