3. 정의
Blocking Queue
요소를 추출할 때는 요소가 한 개 이상 될 때까지는 기다
리고 , 요소를 넣을 때는 Queue에 공간이 넣을 수 있을 때
까지 대기(Blocking)하는 작업을 추가적으로 지원하는
Queue이다.
4. 특징1
● BlockingQueue에서는 여러가지 작업(put, pop등..)에 대해서 4가지
의 방법을 통해 즉시 처리와 대기 처리를 지원하다.
○ Throws Exception
○ Return Special Value (null or false …)
○ Block
○ Time out
Throws exception Special value Blocks Times out
Insert add(e) offer(e) put(e) offer(e, time, unit)
Remove remove() poll() take() poll(time, unit)
Examine element() peek() not applicable not applicable
5. 특징2
용량
Queue 용량에 제약을 할 수 있다.
remainingCapacity를 이용하여 남은 용량을 확인 할수
있다.
용량에 제약을 하지 않은 경우에는
integer.MAX_VALUE 반환
6. Thread-Safe
모든 queuing method들은 내부잠금이나 또다른 동시
성제어를 통해 thread-safe를 한다
그러나 bulk Collection 작업
(addAll/containsAll/retainAll/removeAll)은 별도로 구
현하지 않는 한 반드시 수행되지는 않는다
특징3
7. Method 종류
Format boolean add(E e)
Name add
Description 큐에 용량 제한을 위반하지 않고 insert 가능
한 경우 요소를 insert 한다.
Paramter E e
Return Value treu (성공)
Exception ● IllegalStateException(용량이 제한
되어 insert실패시)
● ClassCastException
● NullPointerException
● IllegalArgumentException
8. Method 종류
Format boolean offer(E e)
Name offer
Description add()와 동일
Paramter E e
Return Value ● treu (성공)
● false (가용 용량이 없어 insert 실패 경
우)
Exception ● ClassCastException
● NullPointerException
● IllegalArgumentException
9. Method 종류
Format void put(E e)
Name put
Description ● 요소를 insert 한다.
● 다만 용량이 없을 경우 wait 한다.
Paramter E e
Return Value 없음
Exception ● ClassCastException
● NullPointerException
● IllegalArgumentException
● InterruptedException (wait중
interrupt시)
10. Method 종류
Format boolean offer(E e, long timeout, TimeUnit unit)
Name offer
Description ● 요소를 insert 한다.
● 다만 용량이 없을 경우 지정한 시간 만큼
wait 한다.
Paramter ● E e
● long timeout
● TimeUnit unit
Return Value ● treu (성공)
● false (지정한 시간동안 isnert 가능 용량이
되지 않아 insert 실패시)
Exception ● ClassCastException
● NullPointerException
● IllegalArgumentException
11. Method 종류
Format E take()
Name take
Description ● Queue Head의 요소를 추출하고
Queue에서 제거한다.
● 요소가 없으면 Queue에 요소가 들어
올때까지 wait
Paramter 없음
Return Value E (Queue Head 요소)
Exception ● InterruptedException(wait중
interrupt 발생시)
12. Method 종류
Format E poll(long timeout, TimeUnit unit)
Name poll
Description ● Queue Head의 요소를 추출하고
Queue에서 제거한다.
● 요소가 없으면 지정한 시간동안
Queue에 요소가 들어올 때까지 wait
Paramter ● long timeout
● TimeUnit unit
Return Value ● E (Queue Head 요소)
● null (지정한 시간 동안 Queue에 요소
가 들어오지 않는 경우)
Exception ● take()와 동일
13. Method 종류
Format int remainingCapacity()
Name remainingCapacity
Description 큐에 Block없이 요소를 추가할수 있는 용량
반환
Paramter 없음
Return Value int (용량 제한이 없는 경우
Integer.MAX_VALUE)
Exception
14. Method 종류
Format boolean remove(Object o)
Name remove
Description Queue에서 매핑되는 요소를 제거한다.
Paramter Object o
Return Value treu (동일요소가 한개 이상일 경우)
Exception ● ClassCastException
● NullPointerException
15. Method 종류
Format int drainTo(Collection<? super E> c, int
maxElemnts)
Name drainTo
Description 전달받은 Collection요소들과 일치하는
Queue안의 모든 요소를 제거한다.
maxElemnts값이 설정된 경우 해당 개수까
지만 처리된다.
Paramter Collection<? super E> c
int maxElements
Return Value int (처리된 요소개수)
Exception ● ClassCastException
● NullPointerException
17. ArrayBlockingQueue
● 고정배열을 사용하여 Queue를 구현한 클래스
● 요소를 저장하는 용량은 생성 시 정의하고 변경이 불가능한 BlockingQueue
● FIFO
● 엑세스 정책을 제공
ArrayBlockingQueue (int capacity) 용량을 지정
ArrayBlockingQueue (int capacity,
boolean fair)
엑세스 정책 지정
true일경우 block된 큐의 순서가 FIFO순으
로 된다.
ArrayBlockingQueue (int capacity,
boolean fair, Collection <? extends e> c)
지정된 Collection으로 초기화 한다.
생성자
21. ConcurrentLinkedQueue
● Linked node를 기반으로 하는 용량을 제한하지 않는 Thread-safe Queue
● FIFO
● Head => 가장 오래된것 , Tail => 가장 최근것
● 공통 Collection에 많은 Thread가 접근할 경우 가장 바람직한 Queue이다.
22. ArrayBlockingQueue Vs LinkedBlockingQueue
● 둘다 lock을 기본으로 사용되는 Queue
● ArrayBlockingQueue가 속도가 더 빠른다.
○ Array를 사용함으로써 Linked node보다 기본 연산이 빠른다.
○ 메모리 할당이 우선 선행되기 때문에 GC를 위한 작업이 비교적 적게 발
생한다.
23. LinkedBlockingQueue VS ConcurrentLinkedQueue
● ConcurrentLinkedQueue는 기본적으로 BlockingQueue의 인터페이스로 구
현된 Queue가 아님
● Blocking이 기본 제공 되지 않음.
● 따라서 put(), take()가 없음
● ConcurrentLinkedQueue가 요소 삽입 속도가 훨씬 빠름
● 따라서 3가지의 block방법으로 구현이 할수 있다.
○ spin
○ lock
○ park