Use of BlockingQueue

Source: Internet
Author: User

This example introduces a special queue: BlockingQueue. If BlockQueue is empty, the operations to retrieve things from BlockingQueue will be blocked and enter the waiting state until BlockingQueue is awakened. similarly, if BlockingQueue is full, any operation that tries to store in will be blocked and enters the waiting state until BlockingQueue has space to wake up and continue the operation.
Key Technical Points for using BlockingQueue are as follows:
1. BlockingQueue:
1) add (anObject): add anObject to BlockingQueue. If BlockingQueue can be accommodated, true is returned. Otherwise, an exception is reported.
2) offer (anObject): if possible, add anObject to BlockingQueue. That is, if BlockingQueue can be accommodated, true is returned; otherwise, false is returned.
3) put (anObject): Add anObject to BlockingQueue. If BlockQueue has no space, the thread that calls this method is blocked until there is space in BlockingQueue to continue.
4) poll (time): removes the first object in BlockingQueue. If it cannot be retrieved immediately, it can wait for the time specified by the time parameter. If it cannot be obtained, null is returned.
5) take (): removes the first object in BlockingQueue. If BlockingQueue is empty, the block enters the waiting state until a new object is added to Blocking.
2. BlockingQueue has four specific implementation classes. Select different implementation classes based on different requirements.
1) ArrayBlockingQueue: Specifies the size of BlockingQueue. Its constructor must include an int parameter to specify its size. Its contained objects are sorted in FIFO (first in first out) Order.
2) LinkedBlockingQueue: A BlockingQueue of an indefinite size. If its constructor carries a specified size parameter, the size of the generated BlockingQueue is limited. If no size parameter is provided, the size of the generated BlockingQueue is determined by Integer. MAX_VALUE. the objects contained in the table are sorted in FIFO (first-in-first-out) Order.
3) PriorityBlockingQueue: similar to sort blockqueue, but the sorting of objects contained in it is determined by the natural sorting order of objects or the Comparator of the constructor.
4) SynchronousQueue: Special BlockingQueue. The operations on this queue must be completed in turn by putting and fetching.
3. Compared with ArrayBlockingQueue, the data structures behind them are different. As a result, the data throughput of LinkedBlockingQueue is larger than that of ArrayBlockingQueue. However, when the number of threads is large, the performance of LinkedBlockingQueue is lower than that of arrayblock.

The following are two examples of using BlockingQueue:

BlockingQueue queue = ArrayBlockingQueue (3 (I = 0; I <2; I ++ (Thread. sleep () (Math. random () * 1000 System. out. println (Thread. currentThread (). getName () + "prepare to put data! "Queue. put (1 System. out. println (Thread. currentThread (). getName () + "already put data," + "queue currently has" + queue. size () + "data"} (Thread. sleep (1000 System. out. println (Thread. currentThread (). getName () + "prepare to retrieve data! "System. out. println (Thread. currentThread (). getName () + "data has been removed," + "queue currently has" + queue. size () + "data "}}

 

                            ExecutorService service =          Business3 business =          service.execute(                                ( i=0;i<50;i++                                   ( i=0;i<50;i++            BlockingQueue subQueue =  ArrayBlockingQueue(1     BlockingQueue mainQueue =  ArrayBlockingQueue(1                                      mainQueue.put(1         }                                  ( i=0;i<10;i++                 System.out.println(Thread.currentThread().getName() + " : " +              subQueue.put(1         }                                               ( i=0;i<5;i++                 System.out.println(Thread.currentThread().getName() + " : " +              mainQueue.put(1         }   }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.