blocking queue
TRANSCRIPT
Blocking Queue
목차
정의특징method 종류BlockingQueue 종류 및 특징
정의
Blocking Queue
요소를 추출할 때는 요소가 한 개 이상 될 때까지는 기다리고 , 요소를 넣을 때는 Queue 에 공간이 넣을 수
있을 때까지 대기 (Blocking) 하는 작업을 추가적으로 지원하는 Queue 이다 .
특징 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
특징 2용량
Queue 용량에 제약을 할 수 있다 .remainingCapacity 를 이용하여 남은 용량을 확인 할수
있다 . 용량에 제약을 하지 않은 경우에는
integer.MAX_VALUE 반환
Thread-Safe 모든 queuing method 들은 내부잠금이나 또다른
동시성제어를 통해 thread-safe 를 한다 그러나 bulk Collection
작업 (addAll/containsAll/retainAll/removeAll) 은 별도로 구현하지 않는 한 반드시 수행되지는 않는다
특징 3
Method 종류Format boolean add(E e)
Name add
Description 큐에 용량 제한을 위반하지 않고 insert 가능 한 경우 요소를 insert 한다 .
Paramter E e
Return Value treu ( 성공 )
Exception ● IllegalStateException( 용량이 제한되어 insert 실패시 )
● ClassCastException● NullPointerException● IllegalArgumentException
Method 종류Format boolean offer(E e)
Name offer
Description add() 와 동일
Paramter E e
Return Value ● treu ( 성공 )● false ( 가용 용량이 없어 insert 실패
경우 )
Exception ● ClassCastException● NullPointerException● IllegalArgumentException
Method 종류Format void put(E e)
Name put
Description ● 요소를 insert 한다 .● 다만 용량이 없을 경우 wait 한다 .
Paramter E e
Return Value 없음
Exception ● ClassCastException● NullPointerException● IllegalArgumentException● InterruptedException (wait 중
interrupt 시 )
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
Method 종류Format E take()
Name take
Description ● Queue Head 의 요소를 추출하고Queue 에서 제거한다 .
● 요소가 없으면 Queue 에 요소가 들어올때까지 wait
Paramter 없음
Return Value E (Queue Head 요소 )
Exception ● InterruptedException(wait 중 interrupt 발생시 )
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() 와 동일
Method 종류Format int remainingCapacity()
Name remainingCapacity
Description 큐에 Block 없이 요소를 추가할수 있는 용량 반환
Paramter 없음
Return Value int ( 용량 제한이 없는 경우Integer.MAX_VALUE)
Exception
Method 종류Format boolean remove(Object o)
Name remove
Description Queue 에서 매핑되는 요소를 제거한다 .
Paramter Object o
Return Value treu ( 동일요소가 한개 이상일 경우 )
Exception ● ClassCastException● NullPointerException
Method 종류Format int drainTo(Collection<? super E> c, int
maxElemnts)
Name drainTo
Description 전달받은 Collection 요소들과 일치하는Queue 안의 모든 요소를 제거한다 .maxElemnts 값이 설정된 경우 해당
개수까지만 처리된다 .
Paramter Collection<? super E> cint maxElements
Return Value int ( 처리된 요소개수 )
Exception ● ClassCastException● NullPointerException
Blocking 종류
● ArrayBlockingQueue● LinkedBlockingDequeue● PriorityBlockingQueue● SychronousQueue● ConcurrentLinkedQueue
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 으로 초기화 한다 .
생성자
LinkedBlockingQueue● 선택적으로 Bound 가능한 Linked-List 로 구현한 Queue● 용량을 지정해 주지 않으면 Integer.MAX_VALUE 로 설정● 용량을 초과하지 않으면 동적으로 node 가 추가 , 용량을 넘으면 block
PriorityBlockingQueue● PriorityQueue 와 동일한 정렬 방식 사용 ( 들어오는 element 상관없이 ASC
정렬 )○ ex )
■ 10, 9, 8, ….. 1 삽입 후 추출하면 1,2,3….10 으로 추출됨● 용량 제한이 없어 삽입에는 block 이 없음● 다만 resource 부족시 오류 발생 (OutofMemory)● 추출에는 Block 이 있음 .
SychronousQueue● insert 와 remove 가 동시에 일어나는 Queue
○ insert 를 하려하는데 꺼내려는 thread 가 없으면 block● 내부 용량을 가지지 않는다 .
ConcurrentLinkedQueue● Linked node 를 기반으로 하는 용량을 제한하지 않는 Thread-safe Queue● FIFO● Head => 가장 오래된것 , Tail => 가장 최근것● 공통 Collection 에 많은 Thread 가 접근할 경우 가장 바람직한 Queue 이
다 .
ArrayBlockingQueue Vs LinkedBlockingQueue
● 둘다 lock 을 기본으로 사용되는 Queue● ArrayBlockingQueue 가 속도가 더 빠른다 .
○ Array 를 사용함으로써 Linked node 보다 기본 연산이 빠른다 .○ 메모리 할당이 우선 선행되기 때문에 GC 를 위한 작업이 비교적 적게
발생한다 .
LinkedBlockingQueue VS ConcurrentLinkedQueue
● ConcurrentLinkedQueue 는 기본적으로 BlockingQueue 의 인터페이스로 구현된 Queue 가 아님
● Blocking 이 기본 제공 되지 않음 .● 따라서 put(), take() 가 없음● ConcurrentLinkedQueue 가 요소 삽입 속도가 훨씬 빠름● 따라서 3 가지의 block 방법으로 구현이 할수 있다 .
○ spin○ lock○ park