2009-05-21 12 views
5

यह सवाल .. मेरी पहले प्रश्नों में से एक के साथ संबंधित हैको अवरुद्ध करने के लिए कतार - अधिक आवश्यकता जानकारी

Previous Post

वहाँ में अवरुद्ध प्रकृति एक लाभ के रूप में उल्लेख किया है।

मैंने अवरोध प्रकृति को प्रदर्शित करने के लिए कुछ सरल कोड विकसित करने की कोशिश की लेकिन मैं अटक गया। मैंने अभी आकार 4 के BlockingQueue बनाने की कोशिश की और 5 तत्व जोड़ने की कोशिश की और java.lang.IllegalStateException के साथ समाप्त हो गया। क्या कोई मुझे BlockingQueue की प्रकृति को अवरुद्ध करने के लिए एक कोड उदाहरण दिखा सकता है?


public static void main(String[] args) { 
    BlockingQueue<String> bq = new LinkedBlockingQueue<String>(4); 

    try { 
     bq.offer("A"); 
     bq.offer("B"); 
     bq.offer("C"); 
     bq.offer("D"); 
     bq.offer("E"); 

     System.out.println("1 = " + bq.take()); 
     System.out.println("2 = " + bq.take()); 
     System.out.println("3 = " + bq.take()); 
     System.out.println("4 = " + bq.take()); 
     System.out.println("5 = " + bq.take()); 
     System.out.println("6 = " + bq.take()); 
    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
} 

मैं इस कोड खंड का इस्तेमाल किया। इस मामले में मैं आकार 4 के साथ 5 तत्वों को कतार में डालने की कोशिश कर रहा हूं। इस मामले में 4 तत्व (ए, बी, सी, डी) को कतार में जोड़ा जाना चाहिए। फिर मैं मुद्रण करते समय take() विधि पर कॉल कर रहा हूं। System.out.println("1 = " + bq.take()); पर कॉल करते समय "ई" को कतार में स्वचालित रूप से डाला नहीं जाना चाहिए? क्योंकि यह एक मुफ्त स्लॉट हो जाता है?

उत्तर

11

आप add, offer, या put साथ जोड़ने रहे थे? मुझे लगता है कि आप add का उपयोग कर रहे थे, क्योंकि यह केवल एक ही है जो IllegalStateException फेंक सकता है; लेकिन यदि आप table पढ़ते हैं, तो आप देखेंगे कि यदि आप अर्थशास्त्र को अवरुद्ध करना चाहते हैं, तो आपको put (और take को निकालने के लिए) का उपयोग करना चाहिए।

संपादित करें: आपके उदाहरण के साथ कुछ समस्याएं हैं।

सबसे पहले मैं सवाल का जवाब दूंगा "जब मैं take() पहली बार कॉल करता हूं तो ई क्यों नहीं डाला जाता है?" जवाब यह है कि जब तक आप take() पर कॉल करते हैं, तो आपने पहले से ही को आजमाया है और ई को सम्मिलित करने के लिए विफल रहा है। अंतरिक्ष खाली होने के बाद कुछ भी सम्मिलित करने के लिए कुछ भी नहीं है।

अब यदि आपने offer() से put() बदल दिया है, put("E") कभी वापस नहीं आएगा। क्यूं कर?चूंकि यह कतार से तत्व को निकालने के लिए कुछ अन्य थ्रेड का इंतजार कर रहा है। याद रखें, BlockingQueues कई धागे तक पहुंचने के लिए डिज़ाइन किए गए हैं। यदि आपके पास एकल-थ्रेडेड एप्लिकेशन है तो अवरुद्ध बेकार है (वास्तव में बेकार से भी बदतर)।

public static void main(String[] args) { 
    final BlockingQueue<String> bq = new LinkedBlockingQueue<String>(4); 

    Runnable producer = new Runnable() { 
     public void run() { 
      try { 
       bq.put("A"); 
       bq.put("B"); 
       bq.put("C"); 
       bq.put("D"); 
       bq.put("E"); 
      } catch (InterruptedException ex) { 
       Thread.currentThread().interrupt(); 
      } 
     } 
    }; 
    Runnable consumer = new Runnable() { 
     public void run() { 
      try { 
       System.out.println("1 = " + bq.take()); 
       System.out.println("2 = " + bq.take()); 
       System.out.println("3 = " + bq.take()); 
       System.out.println("4 = " + bq.take()); 
       System.out.println("5 = " + bq.take()); 
       System.out.println("6 = " + bq.take()); 
      } catch (InterruptedException ex) { 
       Thread.currentThread().interrupt(); 
      } 
     } 
    }; 
    new Thread(producer).start(); 
    new Thread(consumer).start(); 
} 

अब put("E") कॉल वास्तव में, सफल होगा जब तक उपभोक्ता धागा कतार से 'ए' को हटा के बाद से यह अब इंतजार कर सकते हैं:

यहाँ एक बेहतर उदाहरण है। अंतिम take() अभी भी अनन्त रूप से अवरुद्ध होगा, क्योंकि निकालने के लिए कोई छठा तत्व नहीं है।

+0

पहले मैंने एड + पोल का इस्तेमाल किया था। लेकिन फिलहाल मैं कोशिश कर रहा हूं + –

+0

हम्म ले लो .... बिंदु मिला .. अब प्रयोग कर रहा है :) –

+0

मुझे PUT() के लिए टाइमआउट कैसे निर्दिष्ट करना चाहिए? –

2

मिमीर्स ने मुझे इसे हराया: पी (+1)
जो आपको चाहिए, शुभकामनाएं!

नोट: डाल() आपके उदाहरण में असफल हो जाएगा क्योंकि स्थान() उपलब्ध होने तक ब्लॉक() अवरुद्ध हो जाएगा। चूंकि अंतरिक्ष कभी उपलब्ध नहीं है, कार्यक्रम कभी भी निष्पादन जारी नहीं रखता है।

==== वर्ष जवाब ======

एक BlockingQueue एक इंटरफेस है, तो आप implementating वर्गों में से एक का उपयोग करना होगा।

"अवरुद्ध प्रकृति" बस कहता है कि आप अपनी कतार से कुछ अनुरोध कर सकते हैं, और यदि यह खाली है, तो यह ब्लॉक (प्रतीक्षा) में होगा जब तक कि कतार में कुछ जोड़ा नहीं जाता है और फिर प्रसंस्करण जारी रहता है।

ArrayBlockingQueue
DelayQueue
LinkedBlockingQueue
PriorityBlockingQueue
SynchronousQueue

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

//your main collection 
LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>(); 

//Add your values 
lbq.put(100); 
lbq.put(200); 

//take() will actually remove the first value from the collection, 
//or block if no value exists yet. 
//you will either have to interrupt the blocking, 
//or insert something into the queue for the program execution to continue 

int currVal = 0; 
try { 
    currVal = lbq.take(); 
} catch (InterruptedException e) { 
    e.printStackTrace(); 
} 

+0

धन्यवाद .. मैं इस्तेमाल किया "LinkedBlockingQueue" –

+0

भयानक, कि सब कुछ आप की जरूरत है? मैं अभी एक उदाहरण पर काम कर रहा हूं यदि आप अभी भी मुझे इसे पोस्ट करना चाहते हैं तो –

+0

बेहतर पोस्ट करें। बस एक छोटा सा उदाहरण :) –

1

विशेष रूप से आपके प्रश्न का उत्तर देने के लिए: ऑफ़र एक नॉनब्लॉकिंग ऑफ़र कॉल है, इसलिए आपके द्वारा पोस्ट किए गए एक थ्रेडेड विधि में, कॉल करने के लिए कॉल ('ई') पूरी कतार को संशोधित किए बिना झूठ देता है। यदि आपने ब्लॉकिंग डाल ('ई') कॉल का उपयोग किया है, तो यह तब तक सो जाएगा जब तक कि स्थान उपलब्ध न हो जाए। हमेशा के लिए अपने सरल उदाहरण में। पुट को पूरा करने के लिए जगह बनाने के लिए आपको कतार के अलग थ्रेड को पढ़ने की आवश्यकता होगी।

+0

डाल के साथ ऑफ़र की जगह लेता है लेकिन असफल रहा। क्या मुझे पुट() वस्तुओं के लिए एक अलग थ्रेड का उपयोग करने की आवश्यकता है? –

+0

मेरे जवाब में, मैंने कहा: आपको पूरा करने के लिए जगह बनाने के लिए कतार के एक अलग थ्रेड को पढ़ने की आवश्यकता होगी। – lostlogic

+0

हां। बिंदु मिला :) –

0

हाय अधिक जानकारी इस वर्ग

/** 
* Inserts the specified element into this queue if it is possible to do 
* so immediately without violating capacity restrictions, returning 
* {@code true} upon success and throwing an 
* {@code IllegalStateException} if no space is currently available. 
* When using a capacity-restricted queue, it is generally preferable to 
* use {@link #offer(Object) offer}. 
* 
* @param e the element to add 
* @return {@code true} (as specified by {@link Collection#add}) 
* @throws IllegalStateException if the element cannot be added at this 
*   time due to capacity restrictions 
* @throws ClassCastException if the class of the specified element 
*   prevents it from being added to this queue 
* @throws NullPointerException if the specified element is null 
* @throws IllegalArgumentException if some property of the specified 
*   element prevents it from being added to this queue 
*/ 
boolean add(E e); 

/** 
* Inserts the specified element into this queue if it is possible to do 
* so immediately without violating capacity restrictions, returning 
* {@code true} upon success and {@code false} if no space is currently 
* available. When using a capacity-restricted queue, this method is 
* generally preferable to {@link #add}, which can fail to insert an 
* element only by throwing an exception. 
* 
* @param e the element to add 
* @return {@code true} if the element was added to this queue, else 
*   {@code false} 
* @throws ClassCastException if the class of the specified element 
*   prevents it from being added to this queue 
* @throws NullPointerException if the specified element is null 
* @throws IllegalArgumentException if some property of the specified 
*   element prevents it from being added to this queue 
*/ 
boolean offer(E e); 

/** 
* Inserts the specified element into this queue, waiting if necessary 
* for space to become available. 
* 
* @param e the element to add 
* @throws InterruptedException if interrupted while waiting 
* @throws ClassCastException if the class of the specified element 
*   prevents it from being added to this queue 
* @throws NullPointerException if the specified element is null 
* @throws IllegalArgumentException if some property of the specified 
*   element prevents it from being added to this queue 
*/ 
void put(E e) throws InterruptedException; 

/** 
* Inserts the specified element into this queue, waiting up to the 
* specified wait time if necessary for space to become available. 
* 
* @param e the element to add 
* @param timeout how long to wait before giving up, in units of 
*  {@code unit} 
* @param unit a {@code TimeUnit} determining how to interpret the 
*  {@code timeout} parameter 
* @return {@code true} if successful, or {@code false} if 
*   the specified waiting time elapses before space is available 
* @throws InterruptedException if interrupted while waiting 
* @throws ClassCastException if the class of the specified element 
*   prevents it from being added to this queue 
* @throws NullPointerException if the specified element is null 
* @throws IllegalArgumentException if some property of the specified 
*   element prevents it from being added to this queue 
*/ 
boolean offer(E e, long timeout, TimeUnit unit) 
    throws InterruptedException; 

/** 
* Retrieves and removes the head of this queue, waiting if necessary 
* until an element becomes available. 
* 
* @return the head of this queue 
* @throws InterruptedException if interrupted while waiting 
*/ 
E take() throws InterruptedException; 

/** 
* Retrieves and removes the head of this queue, waiting up to the 
* specified wait time if necessary for an element to become available. 
* 
* @param timeout how long to wait before giving up, in units of 
*  {@code unit} 
* @param unit a {@code TimeUnit} determining how to interpret the 
*  {@code timeout} parameter 
* @return the head of this queue, or {@code null} if the 
*   specified waiting time elapses before an element is available 
* @throws InterruptedException if interrupted while waiting 
*/ 
E poll(long timeout, TimeUnit unit) 
    throws InterruptedException; 
संबंधित मुद्दे