sitelink1 https://sungjk.github.io/2016/11/02/Queue.html 
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

Unlike in most collections, the size method is NOT a constant-time operation. Because of the asynchronous nature of these queues, determining the current number of elements requires a traversal of the elements, and so may report inaccurate results if this collection is modified during traversal.

LinkedBlockingQueue는 생성자의 인자에 큐의 용량 capacity 를 명시하여 사이즈를 지정할 수 있는 반면에, ConcurrentLinkedQueue는 큐의 사이즈를 지정할 수 없을 뿐만 아니라 size 메서드는 상수 시간에 호출되지 않아서 큐에 들어있는 원소의 개수를 파악하는 것이 어렵다.

ConcurrentLinkedQueue

This implementation employs an efficient “wait-free” algorithm based on one described in Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms by Maged M. Michael and Michael L. Scott.

자바의 java.util 에서 제공하는 Queue 클래스는 멀티 스레드 환경에서 임계영역(critical section)에 대한 동기화가 적용되어 있지 않다. 그래서 자바는 멀티 스레드 환경에서 컬렉션의 요소를 동시적으로(Concurrent) 처리 할 수 있도록 특별한 컬렉션을 제공한다. java.util.concurrent 패키지에 있는 Queue 구현체인 ConcurrentLinkedQueue 는 Non-blocking lock-free Queue를 위해 Maged M. Michael와 Michael L. Scott의 알고리즘을 기반으로 작성되었다. 그래서 ConcurrentLinkedQueue는 큐에 꺼낼 원소가 없다면 즉시 리턴하고 다른 일을 수행하러 간다. 따라서, ConcurrentLinkedQueue는 생산자-소비자 producer-consumer 모델에서 소비자가 많고 생산자가 하나인 경우에 사용하면 좋다.

여러 개의 쓰레드에서 하나의 Queue 객체에 들어있는 데이터를 꺼내기 위해 queue.poll() 메서드를 호출할 경우, 동일한 실행 결과가 나타날 수 있다. 예를 들어, Queue에 [1, 2, 3]과 같은 데이터가 들어있을 경우, 스레드 3개가 critical section에서 poll() 메서드를 호출하면 각 스레드들은 모두 1이라는 데이터를 가져오고 Queue에는 [2, 3]만 남게 된다. 큐가 비어있을 경우 null을 리턴한다.

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

Queue<Object> queue = new ConcurrentLinkedQueue<Object>();
queue.offer(data);  // put
queue.poll();       // get

LinkedBlockingQueue

The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE.

LinkedBlockingQueue 은 이름에서도 알 수 있듯이 각각의 블로킹 큐가 링크드 노드로 연결된 큐이다. 큐에서 꺼내갈 원소가 없을 경우 해당 쓰레드는 wait 상태에 들어간다. 따라서, LinkedBlockingQueue는 생산자가 많고 하나의 소비자일 경우에 사용하면 좋다. 또한 이 글의 서두에서 언급한 것처럼, LinkedBlockingQueue은 큐의 폭발을 막기 위해 생성자에 큐의 사이즈를 명시할 수 있도록 설계되었다.

LinkedBlockingQueue 내에 있는 데이터를 가져오기 retrieve 위해 poll()과 take() 메소드를 제공한다. 이 두 메소드의 차이점은 큐가 비어있을 때, poll 메소드는 null을 리턴하거나 Timeout을 설정할 수 있는 반면에, take 메소드는 꺼낼 수 있는 원소가 있을 때까지 기다린다(waiting).

import java.util.concurrent.LinkedBlockingQueue

LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();

queue.offer();    // put
queue.take();     // get

참고

번호 제목 글쓴이 날짜 조회 수
173 google-auth-library-oauth2-http 라이브러리 다운로드 황제낙엽 2023.11.19 10
172 firebase-admin-java 라이브러리 다운로드 (firebase admin sdk library) 황제낙엽 2023.11.19 1
171 Enum 활용 (개인블로그, Effective Java) file 황제낙엽 2023.11.02 1
170 Enum 활용 (우아한기술블로그) file 황제낙엽 2023.11.02 5
169 [JsonNode] depth 가 여러 단계인 json data 내부를 조회하는 java code 예제 (from Bard) file 황제낙엽 2023.08.09 20
168 JPA 개요 황제낙엽 2023.07.25 3
167 javax.mail 샘플 몇가지 (테스트 수행전) 황제낙엽 2023.06.26 3
166 java 프로그램으로 회원가입용 인증 메일을 보내는 방법 (from naver / 테스트 성공) file 황제낙엽 2023.06.24 219
165 java 프로그램으로 회원가입용 인증 메일을 보내는 방법 (from bing / 테스트 실패) [1] 황제낙엽 2023.06.23 3
164 base64 encode, decode 황제낙엽 2023.06.12 8
163 BASE64Encoder, BASE64Decoder 의 deprecated 황제낙엽 2023.06.12 1
162 java로 알파벳 대소문자를 랜덤으로 조합하는 코드 만들어줘 (ChatGPT) 황제낙엽 2023.03.28 1
161 구글 클라우드 비전 API 사용하기 (Google Cloud Vision API) 황제낙엽 2023.02.22 8
160 람다식(Lambda Expressions in Java) file 황제낙엽 2022.12.03 2
» ConcurrentLinkedQueue와 LinkedBlockingQueue 황제낙엽 2022.04.06 17
158 java.util.Queue file 황제낙엽 2022.04.06 5382
157 HP-UX, IBM-AIX 황제낙엽 2021.06.23 55
156 람다(Lambda)와 함수형 인터페이스 황제낙엽 2021.05.10 19
155 for, while 등의 loop구문에서 sleep하기 황제낙엽 2020.12.04 55
154 미디어 파일의 metadata를 읽자 (metadata-extractor) file 황제낙엽 2020.08.30 583