Blocking and blocking queue stack

Blocking queue

Content blocking queue is complicated by the new features in Java 5, the interface blocking queue is java.util.concurrent.BlockingQueue, it has multiple implementation classes: ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue other similar usage, specifically to see the JDK document, here a simple example look ArrayBlockingQueue, which implements a bounded queue, when the queue is full, it will block until there are elements of the team, the follow-up of the elements can be added to the queue.

See the examples below:

public  class BlockingQueueTest {
     public  static  void main (String [] args) throws InterruptedException { 
        BlockingQueue <String> bqueue = new new ArrayBlockingQueue with <String> (20 is );
         for ( int I = 0; I <30; I ++ ) {
             // the specified this queue element 
            bqueue.put ( "" + I); 
            System.out.println ( "added element to the blocking queue:" + I);
             IF (I> 18 is ) {
                 // Get the team from the queue head element, and out of its queues 
                System.out.println ( "blocking element is removed from the queue:"+bqueue.take ()); 
            } 
        } 
        System.out.println ( "end run this program, about to exit ----" ); 
    } 
}

As can be seen from the results, when added after the first 20 elements, we first element is removed from a team, so that we can continue to add elements to the queue after each add an element, the element begins with the removal of the first team, so program can perform end.

 

Stack blocked

Blocking and blocking queue stack is similar, except that it is a new feature added in Java 6, blocking stack interfaces java.util.concurrent.BlockingDeque there are many implementation class, using a similar method is also more specific view JDK documentation.

public  class BlockingDequeTest {
     public  static  void main (String [] args) throws InterruptedException { 
        BlockingDeque <String> bDeque = new new LinkedBlockingDeque with <String> (20 is );
         for ( int I = 0; I <30; I ++ ) {
             // the specified blocking element to this stack 
            bDeque.putFirst ( "" + I); 
            System.out.println ( "added to the blocking element stack:" + I);
             IF (I> 18 is ) {
                 // from blocking the stack remove the top element, and it removed 
                System.out.println ( "blocking out the element from the stack:"+bDeque.pollFirst ()); 
            } 
        } 
        System.out.println ( "end run this program, about to exit ----" ); 
    } 
}

As can be seen from the results, when added after the first 20 elements, we are, so that we can continue to add elements to the stack from the top element of the shift, after each add an element, put the top element is removed, so that the program you can perform end.

 

Reference Source:. Jike complicated by new features - blocking and blocking queue stack

Guess you like

Origin www.cnblogs.com/ooo0/p/12456811.html