ArrayBlockingQueue 대 Google Guava 비차단 EvictingQueue 예
게시 됨: 2020-12-29
동시성 유틸리티는 Java에서 매우 놀랍습니다.
java.util.concurrent 패키지에는 다양한 방법으로 사용할 수 있는 많은 유틸리티가 포함되어 있습니다.
이 튜토리얼에서는 java.util.concurrent의 차이점을 살펴보겠습니다. ArrayBlockingQueue
및 com.google.common.collect. EvictingQueue
.
EvictingQueue란 무엇입니까?
EvictingQueue
는 Google's Guava library
일부입니다. 이것은 full queue
에 요소를 추가하려고 할 때 대기열의 head
에서 요소를 자동으로 removes
하는 non-blocking
, bounded
(고정 크기) 대기열입니다.
eclipse-java 프로젝트에 Google의 Guava 라이브러리를 추가하는 방법은 무엇입니까?
maven 프로젝트를 사용하는 경우 pom.xml 파일에 아래 maven 종속성을 추가할 수 있습니다. 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
(고정 크기) 대기열입니다. 요소를 unlimited
으로 저장할 CAN'T
. 이 대기열은 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 ) ; } } |
이 Java 프로그램을 Eclipse 콘솔 또는 IntelliJ IDEA에서 애플리케이션으로 실행하기만 하면 다음과 같은 결과가 표시됩니다.

콘솔 결과:
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 |
위의 프로그램을 실행하는 데 문제가 있으면 알려주십시오.