SlideShare uma empresa Scribd logo
1 de 23
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> c
int 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

Mais conteúdo relacionado

Semelhante a Blocking queue

[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - QueueBill Kim
 
android_thread
android_threadandroid_thread
android_threadhandfoot
 
Collection framework
Collection frameworkCollection framework
Collection frameworkssuser34b989
 
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자Jaewook Byun
 

Semelhante a Blocking queue (6)

[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
 
강의자료5
강의자료5강의자료5
강의자료5
 
android_thread
android_threadandroid_thread
android_thread
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
자료구조 큐
자료구조 큐자료구조 큐
자료구조 큐
 
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
데이터 분석 6 - 나만의 배열 기반 LIST, MyLinkedList를 만들어보자
 

Blocking queue

  • 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
  • 16. Blocking 종류 ● ArrayBlockingQueue ● LinkedBlockingDequeue ● PriorityBlockingQueue ● SychronousQueue ● ConcurrentLinkedQueue
  • 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으로 초기화 한다. 생성자
  • 18. LinkedBlockingQueue ● 선택적으로 Bound가능한 Linked-List로 구현한 Queue ● 용량을 지정해 주지 않으면 Integer.MAX_VALUE로 설정 ● 용량을 초과하지 않으면 동적으로 node가 추가, 용량을 넘으면 block
  • 19. PriorityBlockingQueue ● PriorityQueue 와 동일한 정렬 방식 사용(들어오는 element상관없이 ASC정 렬) ○ ex ) ■ 10, 9, 8, ….. 1 삽입 후 추출하면 1,2,3….10으로 추출됨 ● 용량 제한이 없어 삽입에는 block이 없음 ● 다만 resource 부족시 오류 발생(OutofMemory) ● 추출에는 Block이 있음.
  • 20. SychronousQueue ● insert와 remove가 동시에 일어나는 Queue ○ insert를 하려하는데 꺼내려는 thread가 없으면 block ● 내부 용량을 가지지 않는다.
  • 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