ArrayBlockingQueue 对比Google Guava 非阻塞 EvictingQueue 示例
已发表: 2020-12-29
并发实用程序在 Java 中非常棒。
java.util.concurrent 包包含许多实用程序,我们可以以多种不同的方式使用它们。
在本教程中,我们将讨论 java.util.concurrent 之间的区别。 ArrayBlockingQueue
和 com.google.common.collect。 EvictingQueue
。
什么是驱逐队列?
EvictingQueue
是Google's Guava library
一部分。 这是一个non-blocking
、 bounded
(固定大小)队列,当尝试将元素添加到full queue
时,它会自动从队列head
removes
元素。
如何将 Google 的 Guava 库添加到您的 eclipse-java 项目中?
如果您使用的是 maven 项目,您可以将以下 maven 依赖项添加到 pom.xml 文件中。 如果non-maven
项目——你必须从这里下载它并将库包含到你项目的类路径中。
1 2 3 4 5 |
< dependency > < groupId > com . google . guava < / groupId > < artifactId > guava < / artifactId > < version > 30.1.1 - jre < / version > < / dependency > |
什么是 ArrayBlockingQueue?
它是一个blocking
的、 bounded
(固定大小)队列,将元素内部存储在一个数组中。 它CAN'T
存储unlimited
数量的元素。 此队列对元素进行FIFO
(先进先出)排序。
Read more
:单例队列示例
让我们开始示例
- 创建大小为 10 的 ArrayBlockingQueue
- 创建大小为 10 的 EvictingQueue
- 尝试将 15 个元素添加到队列中
- 对于 EvictingQueue - 它不会抛出任何错误
- 对于 ArrayBlockingQueue——它会抛出
Queue Full
错误
创建类 CrunchifyArrayBlockingQueueVsEvictingQueue.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
package crunchify . com . tutorials ; import com . google . common . collect . EvictingQueue ; import java . util . Queue ; import java . util . concurrent . ArrayBlockingQueue ; /** * @author Crunchify.com * ArrayBlockingQueue Vs. Google Guava Non-Blocking EvictingQueue Example * */ public class CrunchifyArrayBlockingQueueVsEvictingQueue { private static void CrunchifyArrayBlockingQueue ( ) { // ArrayBlockingQueue: A bounded blocking queue backed by an array. This queue orders elements FIFO (first-in-first-out). // The head of the queue is that element that has been on the queue the longest time. // The tail of the queue is that element that has been on the queue the shortest time. // New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. ArrayBlockingQueue < String > crunchifyQueue = new ArrayBlockingQueue < String > ( 10 ) ; String crunchifyMsg = "This is ArrayBlockingQueue - by Crunchify" ; try { // We are looping for 15 times - Error once queue full for ( int counter = 1 ; counter < = 15 ; counter ++ ) { // add(): Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, // returning true upon success and throwing an IllegalStateException if this queue is full. crunchifyQueue . add ( crunchifyMsg + counter ) ; // size(): Returns the number of elements in this queue. crunchifyLog ( "ArrayBlockingQueue size: " + crunchifyQueue . size ( ) ) ; } } catch ( Exception e ) { crunchifyLog ( "\nException Occurred: " ) ; e . printStackTrace ( ) ; } } public static void main ( String [ ] args ) { // Test EvictingQueue with size 10 CrunchifyEvictingQueue ( ) ; crunchifyLog ( "\n============= New LINE ==============\n" ) ; // Test ArrayBlockingQueue with size 10 CrunchifyArrayBlockingQueue ( ) ; } private static void CrunchifyEvictingQueue ( ) { // Queue: A collection designed for holding elements prior to processing. Besides basic Collection operations, // queues provide additional insertion, extraction, and inspection operations. // Each of these methods exists in two forms: one throws an exception if the operation fails, // the other returns a special value (either null or false, depending on the operation). Queue < String > crunchifyQueue = EvictingQueue . create ( 10 ) ; String crunchifyMsg = "This is EvictingQueue - by Crunchify" ; try { // We are looping for 15 times - No error after queue full. // Instead, it will remove element from queue in FIFO order for ( int i = 1 ; i < = 15 ; i ++ ) { // add: Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, // returning true upon success and throwing an IllegalStateException if no space is currently available. crunchifyQueue . add ( crunchifyMsg + i ) ; crunchifyLog ( "EvictingQueue size: " + crunchifyQueue . size ( ) ) ; } } catch ( Exception e ) { crunchifyLog ( "Exception Occurred: " + e ) ; } } private static void crunchifyLog ( String crunchifyText ) { System . out . println ( crunchifyText ) ; } } |
只需在 Eclipse 控制台或 IntelliJ IDEA 中将这个 Java 程序作为应用程序运行,您就会看到这样的结果。

控制台结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
EvictingQueue size : 1 EvictingQueue size : 2 EvictingQueue size : 3 EvictingQueue size : 4 EvictingQueue size : 5 EvictingQueue size : 6 EvictingQueue size : 7 EvictingQueue size : 8 EvictingQueue size : 9 EvictingQueue size : 10 EvictingQueue size : 10 EvictingQueue size : 10 EvictingQueue size : 10 EvictingQueue size : 10 EvictingQueue size : 10 ============= New LINE ============== ArrayBlockingQueue size : 1 ArrayBlockingQueue size : 2 ArrayBlockingQueue size : 3 ArrayBlockingQueue size : 4 ArrayBlockingQueue size : 5 ArrayBlockingQueue size : 6 ArrayBlockingQueue size : 7 ArrayBlockingQueue size : 8 ArrayBlockingQueue size : 9 ArrayBlockingQueue size : 10 Exception Occurred : java . lang . IllegalStateException : Queue full at java . base / java . util . AbstractQueue . add ( AbstractQueue . java : 98 ) at java . base / java . util . concurrent . ArrayBlockingQueue . add ( ArrayBlockingQueue . java : 329 ) at crunchify . com . tutorials . CrunchifyArrayBlockingQueueVsEvictingQueue . CrunchifyArrayBlockingQueue ( CrunchifyArrayBlockingQueueVsEvictingQueue . java : 32 ) at crunchify . com . tutorials . CrunchifyArrayBlockingQueueVsEvictingQueue . main ( CrunchifyArrayBlockingQueueVsEvictingQueue . java : 52 ) Process finished with exit code 0 |
如果您在运行上述程序时遇到任何问题,请告诉我。