Queue blocking queue LinkedBlockingQueue

  

  ArrayBlockingQueue except for the data storage structure, the size can not be specified, are consistent with the other 

 

Package com.dh.learn.queue; 

Import java.util.concurrent.BlockingQueue;
 Import java.util.concurrent.LinkedBlockingQueue; 

public  class LearnLinkedBlockingQueue {
     // underlying list is implemented
     // achieve ArrayBlockingQueue consistent with the blocking mode:
     //       ReenTrantLock thread-safe
     //       for condition Condition of await () single () guaranteed queue blocking 
    public  static  void main (String [] args) throws InterruptedException {
         // can specify the size of the list, is not specified the default Integer.MAX_VALUE 31 times (2 side -1) 
        BlockingQueue <String> = LinkedBlockingQueue new new a LinkedBlockingQueue <> (2);
        linkedBlockingQueue.put("aaaa");
        linkedBlockingQueue.put("bbbb");

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("取值,使队列不满");
                    linkedBlockingQueue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        linkedBlockingQueue.put("cccc");

        System.out.println(linkedBlockingQueue.toString());
    }
}

 

Guess you like

Origin www.cnblogs.com/han6/p/11275187.html