PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Inter-Thread Communication: wait(), notify(), notifyAll() (इंटर-थ्रेड संचार: wait(), notify() और notifyAll() (Object क्लास)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Inter-thread communication allows cooperating threads to pause and signal each other cleanly without wasteful busy-waiting loops using wait(), notify(), and notifyAll() defined in java.lang.Object. These methods require owning the target object's monitor lock inside a synchronized context and coordinate classic Producer-Consumer workflows.
अध्याय सार: इंटर-थ्रेड कम्युनिकेशन सहयोगी थ्रेड्स को व्यर्थ के बिज़ी-वेटिंग लूप के बिना एक-दूसरे को रोकने व सिग्नल देने की सुविधा देता है, जिसके लिए java.lang.Object क्लास में wait(), notify() और notifyAll() मेथड्स दिए गए हैं। इनका उपयोग केवल synchronized ब्लॉक या मेथड के भीतर मॉनिटर लॉक के साथ ही किया जा सकता है।
Inter-Thread Communication: wait(), notify(), notifyAll(): Detailed Notes & Architecture (इंटर-थ्रेड संचार: wait(), notify() और notifyAll() (Object क्लास))
1. Why wait(), notify(), and notifyAll() Live in java.lang.Object (wait(), notify() और notifyAll() Object क्लास में क्यों हैं?)
[EN] Although used for thread coordination, wait(), notify(), and notifyAll() are final methods in java.lang.Object rather than java.lang.Thread. This is because in Java any shared object—such as a queue, buffer, or lock token—acts as a monitor with both an entry lock and an associated wait-set of paused threads. Threads communicate through the shared resource's monitor rather than invoking coordination directly on Thread objects. Calling any of these three methods without owning that object's monitor lock throws IllegalMonitorStateException.
[HI] यद्यपि ये थ्रेड्स के समन्वय के लिए काम आते हैं, फिर भी wait(), notify() और notifyAll() Thread क्लास के बजाय java.lang.Object में फाइनल मेथड्स के रूप में परिभाषित हैं। इसका कारण यह है कि जावा में कोई भी साझा ऑब्जेक्ट मॉनिटर लॉक और वेट-सेट रख सकता है। बिना उस ऑब्जेक्ट का मॉनिटर लॉक प्राप्त किए इन्हें कॉल करने पर IllegalMonitorStateException आता है।
2. wait() Mechanics and Lock Release vs Thread.sleep() (wait() की कार्यप्रणाली और लॉक रिलीज बनाम sleep())
[EN] When a thread inside `synchronized(obj)` calls `obj.wait()`, the JVM immediately releases the intrinsic lock on `obj` and places the thread into `obj`'s wait-set in `WAITING` state (or `TIMED_WAITING` for `wait(ms)`). Releasing the lock is essential so another thread (such as a Producer or Consumer) can enter `synchronized(obj)` and modify the condition! By stark contrast, `Thread.sleep(ms)` is a static Thread method that keeps all locks held while pausing. Note that `wait()` only releases the lock of the object on which `wait()` is called—other outer locks held by the thread stay locked.
[HI] जब कोई थ्रेड `synchronized(obj)` के भीतर `obj.wait()` कॉल करता है, तो वह तुरंत `obj` का मॉनिटर लॉक छोड़ देता है और WAITING (या `wait(ms)` के लिए TIMED_WAITING) अवस्था में चला जाता है। लॉक छोड़ना इसलिए जरूरी है ताकि दूसरा थ्रेड `synchronized(obj)` में प्रवेश कर सके। इसके विपरीत, `Thread.sleep()` स्टैटिक मेथड है और लॉक को पकड़े रहता है।
3. notify() vs notifyAll() and Lock Re-acquisition (notify() बनाम notifyAll() और लॉक की पुनः प्राप्ति)
[EN] `obj.notify()` wakes up a single arbitrary thread waiting in `obj`'s wait-set (chosen by the JVM/OS), whereas `obj.notifyAll()` wakes up every waiting thread on `obj`. Crucially, neither `notify()` nor `notifyAll()` immediately hands over the monitor lock: the notified thread moves from WAITING to BLOCKED and can only resume after the notifying thread finishes and exits its `synchronized(obj)` block! Because `notify()` can wake the wrong thread when multiple conditions share one object (e.g., Producers and Consumers sharing one buffer), `notifyAll()` is safer to prevent missed signals and deadlocks.
[HI] `obj.notify()` वेट-सेट में प्रतीक्षारत किसी एक अज्ञात थ्रेड को जगाता है, जबकि `obj.notifyAll()` प्रतीक्षा कर रहे सभी थ्रेड्स को जगा देता है। महत्वपूर्ण बात यह है कि notify() तुरंत लॉक नहीं छोड़ता; जगाया गया थ्रेड तब तक ब्लॉक रहता है जब तक notify करने वाला थ्रेड synchronized ब्लॉक से बाहर न निकल जाए। कई प्रोड्यूसर व कंज्यूमर वाले बफर में सिग्नल छूटने से बचने के लिए `notifyAll()` अधिक सुरक्षित होता है।
4. Producer-Consumer Pattern, Spurious Wakeups, and Condition (प्रोड्यूसर-कंज्यूमर पैटर्न, स्पूरियस वेकअप और Condition API)
[EN] In the classic Producer-Consumer pattern over a bounded buffer, a Producer calls `wait()` while the buffer is full and calls `notifyAll()` after adding an item; a Consumer calls `wait()` while empty and calls `notifyAll()` after removing an item. Always guard `wait()` inside a `while (!condition)` loop—never an `if` statement—because OS threads can suffer rare spurious wakeups or another competing thread might consume the item before the awakened thread re-acquires the lock. Modern Java also provides `ReentrantLock.newCondition()` with separate `notEmpty` and `notFull` `Condition` queues using `await()`, `signal()`, and `signalAll()`.
[HI] बाउंडेड बफर के प्रोड्यूसर-कंज्यूमर पैटर्न में प्रोड्यूसर बफर फुल होने पर wait() करता है और आइटम जोड़कर notifyAll() भेजता है। `wait()` को हमेशा `if` के बजाय `while (!condition)` लूप में लिखना चाहिए ताकि स्पूरियस वेकअप (Spurious Wakeup) या अन्य थ्रेड द्वारा स्थिति बदल देने से बग न आए। आधुनिक जावा में अलग-अलग वेट-सेट के लिए `ReentrantLock` के साथ `Condition` (`await()`, `signal()`, `signalAll()`) का उपयोग किया जाता है।
Syntax, Formula & Technical Example (Inter-Thread Communication: wait(), notify(), notifyAll())
class SharedBuffer {
private int data; private boolean hasData = false;
public synchronized void produce(int val) throws InterruptedException {
while (hasData) wait(); // Always use 'while', not 'if'
data = val; hasData = true;
notifyAll(); // Wakes waiting consumers after monitor exit
}
public synchronized int consume() throws InterruptedException {
while (!hasData) wait();
hasData = false; notifyAll(); return data;
}
}
Inter-Thread Communication: wait(), notify(), notifyAll() Quick Reference & Comparison Table
| Coordination Method | Class & Thread State Effect | Monitor Lock & Synchronization Rule |
|---|---|---|
| obj.wait() | Object final method; enters WAITING / TIMED_WAITING | Releases obj monitor lock immediately; requires synchronized(obj) |
| obj.notify() | Object final method; wakes 1 arbitrary waiting thread | Retains lock until block exit; requires synchronized(obj) |
| obj.notifyAll() | Object final method; wakes all threads in wait-set | Prevents missed signals in multi-role queues; requires synchronized |
| Thread.sleep(ms) | Thread static method; enters TIMED_WAITING | Retains all held monitor locks; no synchronized context needed |
Inter-Thread Communication: wait(), notify(), notifyAll() Top 20 Bilingual MCQs with Answers
Q1. In which class are the wait(), notify(), and notifyAll() methods defined in Java?
(जावा में wait(), notify() और notifyAll() मेथड्स किस क्लास में परिभाषित हैं?)
(A) java.lang.Thread
(B) java.lang.Runnable
(C) java.lang.Object
(D) java.util.concurrent.Lock
Correct Answer: (C)
[EN] They belong to java.lang.Object because every Java object has an intrinsic monitor lock and wait-set.
[HI] ये मेथड्स java.lang.Object में परिभाषित हैं क्योंकि जावा के प्रत्येक ऑब्जेक्ट में एक मॉनिटर लॉक और वेट-सेट होता है।
Q2. Which runtime exception is thrown if wait(), notify(), or notifyAll() is called outside a synchronized block/method on that object?
(यदि किसी ऑब्जेक्ट के synchronized ब्लॉक या मेथड के बाहर wait(), notify() या notifyAll() कॉल किया जाए तो कौन सा एक्सेप्शन आता है?)
(A) IllegalThreadStateException
(B) InterruptedException
(C) IllegalMonitorStateException
(D) MonitorLockException
Correct Answer: (C)
[EN] The calling thread must own the target object's intrinsic monitor lock; otherwise JVM throws IllegalMonitorStateException.
[HI] मॉनिटर लॉक के बिना इन मेथड्स को कॉल करने पर रनटाइम में IllegalMonitorStateException उत्पन्न होता है।
Q3. What is the key difference between obj.wait() and Thread.sleep(1000) regarding monitor locks?
(मॉनिटर लॉक के संबंध में obj.wait() और Thread.sleep(1000) के बीच मुख्य अंतर क्या है?)
(A) sleep() releases all locks, whereas wait() retains them
(B) Both release all locks immediately
(C) Neither method affects monitor locks
(D) wait() releases the object's monitor lock, whereas sleep() retains all held locks
Correct Answer: (D)
[EN] wait() must release the lock so another thread can enter synchronized(obj) and call notify(); sleep() keeps locks held.
[HI] wait() ऑब्जेक्ट का मॉनिटर लॉक छोड़ देता है ताकि दूसरा थ्रेड notify() कर सके, जबकि sleep() लॉक पकड़े रहता है।
Q4. When Thread A calls obj.notify() inside a synchronized(obj) block, when can the awakened Thread B actually enter the synchronized block?
(जब थ्रेड A synchronized(obj) ब्लॉक में obj.notify() कॉल करता है, तो जागा हुआ थ्रेड B वास्तव में कब चलना शुरू कर सकता है?)
(A) Only when garbage collection runs
(B) Only after Thread A exits the synchronized(obj) block and releases the monitor lock
(C) Only after calling Thread.yield()
(D) Immediately inside the notify() line before Thread A finishes
Correct Answer: (B)
[EN] notify() does not release the monitor lock; Thread B moves from WAITING to BLOCKED until Thread A leaves synchronized.
[HI] notify() लॉक नहीं छोड़ता; थ्रेड B को तब तक इंतजार करना पड़ता है जब तक थ्रेड A अपना synchronized ब्लॉक पूरा न कर ले।
Q5. Why should wait() always be called inside a `while (!condition)` loop instead of an `if (!condition)` check?
(wait() को हमेशा `if (!condition)` के बजाय `while (!condition)` लूप के अंदर क्यों कॉल किया जाना चाहिए?)
(A) Because `if` cannot run inside synchronized methods
(B) Because `while` automatically converts wait() into notifyAll()
(C) To re-check the condition against spurious wakeups and competing threads altering state
(D) Because `if` causes a compile-time error with wait()
Correct Answer: (C)
[EN] Spurious wakeups or another thread consuming the resource first can leave the condition false when wait() returns.
[HI] स्पूरियस वेकअप या किसी अन्य थ्रेड द्वारा स्थिति बदल देने पर भी `while` लूप शर्त की दोबारा पुष्टि कर लेता है।
Q6. Which checked exception must be handled or declared when calling obj.wait()?
(obj.wait() कॉल करते समय किस चेक्ड एक्सेप्शन को हैंडल या throws करना अनिवार्य होता है?)
(A) IllegalMonitorStateException
(B) TimeoutException
(C) InterruptedException
(D) IOException
Correct Answer: (C)
[EN] wait(), sleep(), and join() declare `throws InterruptedException` because another thread can interrupt the waiting thread.
[HI] wait() `throws InterruptedException` घोषित करता है (यह एक चेक्ड एक्सेप्शन है), जबकि IllegalMonitorStateException अनचेक्ड है।
Q7. What happens when obj.wait(0) is called?
(obj.wait(0) कॉल करने पर क्या होता है?)
(A) It behaves identically to wait() and waits indefinitely until notified or interrupted
(B) It returns immediately in 0 milliseconds without releasing the lock
(C) It throws IllegalArgumentException
(D) It waits for 0 nanoseconds and skips the wait-set
Correct Answer: (A)
[EN] Per Javadoc specification, wait(0) means wait indefinitely with no timeout, identical to no-arg wait().
[HI] जावा डॉक्यूमेंटेशन के अनुसार, wait(0) बिना टाइमआउट के अनिश्चित काल तक (wait() की तरह) प्रतीक्षा करता है।
Q8. Why is notifyAll() often safer than notify() in multi-producer, multi-consumer shared queues?
(मल्टी-प्रोड्यूसर और मल्टी-कंज्यूमर साझा क्यू में notify() की तुलना में notifyAll() अधिक सुरक्षित क्यों होता है?)
(A) notify() throws an exception if more than two threads are waiting
(B) notifyAll() skips lock acquisition overhead
(C) notify() wakes one arbitrary thread (possibly another producer instead of a consumer), risking missed signals or deadlock
(D) notify() only works on static methods
Correct Answer: (C)
[EN] When multiple roles share a single wait-set, notify() might wake a same-type thread that goes back to sleep without waking the needed role.
[HI] यदि प्रोड्यूसर ने गलती से कंज्यूमर के बजाय दूसरे प्रोड्यूसर को जगा दिया तो सिस्टम अटक सकता है; notifyAll() सभी को अवसर देता है।
Q9. How many overloaded versions of wait() exist in java.lang.Object?
(java.lang.Object क्लास में wait() मेथड के कितने ओवरलोडेड संस्करण मौजूद हैं?)
(A) 3 (wait(), wait(long timeoutMillis), and wait(long timeoutMillis, int nanos))
(B) 2 (wait() and wait(long))
(C) 1 (only wait())
(D) 4 (including wait(TimeUnit))
Correct Answer: (A)
[EN] Object defines wait(), wait(long), and wait(long, int)—all three are public final methods.
[HI] Object में wait() के तीन ओवरलोडेड वर्जन हैं: wait(), wait(long) और wait(long, int)।
Q10. Which thread wakes up when notify() is called while three threads T1, T2, and T3 are waiting on the same object?
(यदि तीन थ्रेड T1, T2 और T3 एक ही ऑब्जेक्ट पर प्रतीक्षा कर रहे हों और notify() कॉल किया जाए, तो कौन सा थ्रेड जागेगा?)
(A) All three threads wake up sequentially
(B) Exactly one thread chosen arbitrarily by the JVM/OS thread scheduler (not guaranteed FIFO)
(C) Always the thread with highest priority
(D) Always the first thread T1 in strict FIFO order
Correct Answer: (B)
[EN] The Java Language Specification leaves wait-set selection to the JVM implementation without guaranteeing FIFO order.
[HI] जावा स्पेसिफिकेशन के अनुसार, JVM कोई भी एक थ्रेड मनमाने ढंग से चुनता है; FIFO क्रम की कोई गारंटी नहीं होती।
Q11. What happens if notify() is called before any thread has called wait() on that object?
(यदि किसी थ्रेड के wait() कॉल करने से पहले ही notify() कॉल कर दिया जाए, तो क्या होता है?)
(A) The notification is lost (no effect), and a later wait() will block until a future notify()
(B) Throws NoSuchElementException
(C) Throws IllegalMonitorStateException even inside synchronized
(D) The notification is buffered and immediately wakes the next wait() call
Correct Answer: (A)
[EN] Object wait/notify does not retain permits like Semaphore; missed notifications are simply discarded.
[HI] notify() कोई सिग्नल स्टोर नहीं करता; पहले भेजा गया सिग्नल नष्ट हो जाता है और बाद वाला wait() प्रतीक्षा करता रहेगा।
Q12. In modern java.util.concurrent.locks, which interface provides `await()`, `signal()`, and `signalAll()` bound to a `Lock`?
(आधुनिक java.util.concurrent.locks में कौन सा इंटरफेस `Lock` के साथ `await()`, `signal()` और `signalAll()` मेथड्स देता है?)
(A) CyclicBarrier
(B) Condition (created via lock.newCondition())
(C) CountDownLatch
(D) Semaphore
Correct Answer: (B)
[EN] Condition replaces Object monitor methods when using explicit ReentrantLock instances, supporting multiple separate wait-sets per lock.
[HI] ReentrantLock.newCondition() से बनने वाला Condition ऑब्जेक्ट अलग-अलग वेट-सेट के लिए await(), signal(), signalAll() देता है।
Q13. If Thread A holds locks on BOTH `obj1` and `obj2`, and calls `obj2.wait()`, which locks are released?
(यदि थ्रेड A ने `obj1` और `obj2` दोनों के लॉक पकड़े हैं और वह `obj2.wait()` कॉल करता है, तो कौन सा लॉक छूटेगा?)
(A) Only `obj1` is released
(B) Neither lock is released
(C) Both `obj1` and `obj2` locks are released
(D) Only the lock on `obj2` is released; `obj1` remains locked by Thread A
Correct Answer: (D)
[EN] wait() releases ONLY the monitor lock of the target object on which wait() was invoked; holding outer locks can cause deadlocks.
[HI] wait() केवल उसी ऑब्जेक्ट (`obj2`) का लॉक छोड़ता है जिस पर उसे कॉल किया गया है; बाहरी लॉक (`obj1`) बंद रहता है।
Q14. Which blocking collection in java.util.concurrent automatically handles Producer-Consumer wait/notify coordination internally?
(java.util.concurrent में कौन सा इंटरफेस आंतरिक रूप से प्रोड्यूसर-कंज्यूमर wait/notify समन्वय को स्वतः संभालता है?)
(A) BlockingQueue (e.g., ArrayBlockingQueue, LinkedBlockingQueue)
(B) PriorityQueue
(C) ConcurrentHashMap
(D) CopyOnWriteArrayList
Correct Answer: (A)
[EN] BlockingQueue implementations block `put()` when full and `take()` when empty using ReentrantLock + Conditions internally.
[HI] ArrayBlockingQueue जैसे BlockingQueue अंदरूनी रूप से लॉक और कंडीशन का उपयोग करके put() व take() को थ्रेड-सेफ बनाते हैं।
Q15. What happens if a subclass tries to override wait() or notify() in Java?
(यदि कोई सबक्लास जावा में wait() या notify() को ओवरराइड करने का प्रयास करे, तो क्या होगा?)
(A) Compile-time error because wait(), notify(), and notifyAll() are declared `final` in Object
(B) Allowed only inside Thread subclasses
(C) Throws SecurityException at runtime
(D) Compiles normally and replaces Object's behavior
Correct Answer: (A)
[EN] Object marks wait(), notify(), notifyAll(), and getClass() as `final` so core synchronization semantics cannot be broken.
[HI] Object क्लास में wait(), notify() और notifyAll() को `final` घोषित किया गया है, इसलिए इन्हें ओवरराइड करने पर कंपाइल एरर आता है।
Q16. Which of the following statements about wait(), notify(), and notifyAll() is NOT true?
(wait(), notify() और notifyAll() के बारे में निम्नलिखित में से कौन सा कथन असत्य (NOT true) है?)
(A) Calling `notify()` immediately transfers CPU execution and monitor ownership to the waiting thread
(B) Spurious wakeups make `while` loops preferable over `if` checks around `wait()`
(C) All three methods are defined as final methods inside java.lang.Object
(D) Calling `wait()` outside a synchronized context throws IllegalMonitorStateException
Correct Answer: (A)
[EN] The notifying thread keeps the monitor lock until it exits its synchronized block/method.
[HI] `notify()` कॉल करने वाला थ्रेड तुरंत लॉक नहीं छोड़ता; जब तक वह synchronized ब्लॉक से बाहर न निकले, जागा हुआ थ्रेड आगे नहीं बढ़ सकता।
Q17. Within Inter-Thread Communication: wait(), notify(), notifyAll(), which coordination method directly maps to: "Object final method; enters WAITING / TIMED_WAITING"?
(Inter-Thread Communication: wait(), notify(), notifyAll() में कौन सा Coordination Method सीधे "Object final method; enters WAITING / TIMED_WAITING" से संबंधित है?)
(A) Thread.sleep(ms)
(B) obj.notify()
(C) obj.notifyAll()
(D) obj.wait()
Correct Answer: (D)
[EN] In Inter-Thread Communication: wait(), notify(), notifyAll(), 'obj.wait()' handles 'Object final method; enters WAITING / TIMED_WAITING' (Releases obj monitor lock immediately; requires synchronized(obj)).
[HI] Inter-Thread Communication: wait(), notify(), notifyAll() में 'obj.wait()' का कार्य/विवरण 'Object final method; enters WAITING / TIMED_WAITING' (Releases obj monitor lock immediately; requires synchronized(obj)) है।
Q18. Which option accurately describes 'obj.notify()' in Inter-Thread Communication: wait(), notify(), notifyAll()?
(Inter-Thread Communication: wait(), notify(), notifyAll() के संदर्भ में 'obj.notify()' की सही विशेषता कौन सी है?)
(A) Thread static method; enters TIMED_WAITING
(B) Object final method; wakes 1 arbitrary waiting thread
(C) Object final method; wakes all threads in wait-set
(D) Object final method; enters WAITING / TIMED_WAITING
Correct Answer: (B)
[EN] 'obj.notify()' provides 'Object final method; wakes 1 arbitrary waiting thread' — key point: Retains lock until block exit; requires synchronized(obj).
[HI] 'obj.notify()' का मुख्य विवरण 'Object final method; wakes 1 arbitrary waiting thread' है (Retains lock until block exit; requires synchronized(obj))।
Q19. In Inter-Thread Communication: wait(), notify(), notifyAll(), identify the correct parameter for the specification: "Object final method; wakes all threads in wait-set".
(Inter-Thread Communication: wait(), notify(), notifyAll() में "Object final method; wakes all threads in wait-set" विवरण किस विकल्प के लिए सही है?)
(A) obj.notifyAll()
(B) obj.notify()
(C) obj.wait()
(D) Thread.sleep(ms)
Correct Answer: (A)
[EN] 'obj.notifyAll()' corresponds to 'Object final method; wakes all threads in wait-set' (Prevents missed signals in multi-role queues; requires synchronized).
[HI] 'obj.notifyAll()' का संबंध 'Object final method; wakes all threads in wait-set' (Prevents missed signals in multi-role queues; requires synchronized) से है।
Q20. Considering Inter-Thread Communication: wait(), notify(), notifyAll(), what key behavior defines 'Thread.sleep(ms)'?
(Inter-Thread Communication: wait(), notify(), notifyAll() में 'Thread.sleep(ms)' की मुख्य भूमिका या मान क्या है?)
(A) Object final method; enters WAITING / TIMED_WAITING
(B) Object final method; wakes 1 arbitrary waiting thread
(C) Object final method; wakes all threads in wait-set
(D) Thread static method; enters TIMED_WAITING
Correct Answer: (D)
[EN] 'Thread.sleep(ms)' represents 'Thread static method; enters TIMED_WAITING' (Retains all held monitor locks; no synchronized context needed).
[HI] 'Thread.sleep(ms)' का अर्थ/मान 'Thread static method; enters TIMED_WAITING' है (Retains all held monitor locks; no synchronized context needed)।
Inter-Thread Communication: wait(), notify(), notifyAll() — Exam FAQs
Q1: Why shouldn't we use a busy-waiting while loop instead of wait()/notify()?
wait()/notify() के बजाय साधारण busy-waiting while लूप का उपयोग क्यों नहीं करना चाहिए?
[EN] Busy-waiting spins continuously at 100% CPU wasting power and scheduler time, whereas wait() puts the thread into zero-CPU WAITING state until an event occurs.
[HI] बिज़ी-वेटिंग लगातार CPU चक्र बर्बाद करता है, जबकि wait() थ्रेड को बिना CPU खर्च किए शांत (WAITING) अवस्था में रखकर सिग्नल आने पर ही जगाता है।
Q2: Can wait() and notify() be used to communicate between two completely separate JVM processes?
क्या wait() और notify() का उपयोग दो अलग-अलग JVM प्रोसेस के बीच संचार के लिए किया जा सकता है?
[EN] No. Intrinsic monitors and wait-sets exist purely within a single JVM process's heap; cross-process communication requires sockets, pipes, files, or message queues.
[HI] नहीं। मॉनिटर लॉक्स एक ही JVM प्रोसेस की मेमोरी के भीतर काम करते हैं; अलग प्रोसेस के बीच संचार के लिए सॉकेट्स या मैसेज क्यू चाहिए।
Q3: What is the difference between Thread.join() and Object.wait()?
Thread.join() और Object.wait() के बीच क्या अंतर है?
[EN] t.join() waits specifically for thread t to terminate (implemented internally using wait() on the Thread object until !isAlive()), whereas obj.wait() waits on an arbitrary shared object for an explicit notify()/notifyAll().
[HI] t.join() विशेष रूप से थ्रेड t के समाप्त होने का इंतजार करता है, जबकि obj.wait() किसी भी साझा ऑब्जेक्ट पर notify()/notifyAll() सिग्नल का इंतजार करता है।
Post a Comment
Post a Comment