निम्नलिखित कोड JavaDoc of Condition
से लिया जाता है एक रैत्रांत में एक शर्त पर प्रतीक्षा कर रहा: का एक उदाहरण परताला
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}
कल्पना कीजिए 2 सूत्र, उपभोक्ता और निर्माता, एक take
का उपयोग कर, एक put
BoundedBuffer
।
मान लीजिए कि उपभोक्ता पहले चला जाता है, take()
जिसमें उन्होंने lock
ताले और अब notEmpty.await();
पर लूप तक चलता है।
कैसे कर सकता है अब निर्माता संभवतः lock
, जो पहले से ही उपभोक्ता द्वारा आयोजित किया जाता है ताला लगा अतीत put()
विधि में मिल सकता है?
मुझे यहां क्या याद आ रही है? क्या lock
"अस्थायी रूप से जारी किया गया है" जबकि थ्रेड इसकी स्थितियों में से किसी एक पर इंतजार कर रहा है? और ताला मतलब के पुनर्वित्त क्या है, बिल्कुल?
मैं के बारे में था पर 10-15% से कम है दो के साथ बिल्कुल वैसा ही सवाल पूछते हैं JavaDoc पर बिल्कुल वही उदाहरण :) मुझे समय खोने से बचाया। – Bren