๋น์ฐจ๋จ ๋๊ธฐ์ด
![](https://cdn.codegym.cc/images/article/128e424c-63bb-47db-9d23-7ce3ae97fa4a/512.jpeg)
์ฐ๊ฒฐ๋ ๋ ธ๋์์ ์ค๋ ๋ ์์ ํ๊ณ ๊ฐ์ฅ ์ค์ํ ๋น์ฐจ๋จ Queue ๊ตฌํ.
ConcurrentLinkedQueue<E> - ๊ฐ๋น์ง ์์ง๊ธฐ์ ํจ๊ป ์๋ํ๋๋ก ์กฐ์ ๋ ๋๊ธฐ ์๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํฉ๋๋ค. ์ด ์๊ณ ๋ฆฌ์ฆ์ CAS๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ตฌ์ถ๋์๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ํจ์จ์ ์ด๊ณ ๋งค์ฐ ๋น ๋ฆ ๋๋ค. size() ๋ฉ์๋๋์ค๋ซ๋์ ์คํ๋ ์ ์์ผ๋ฏ๋ก ํญ์ ๋์ด์ค์ง ์๋ ๊ฒ์ด ๊ฐ์ฅ ์ข์ต๋๋ค.
ConcurrentLinkedDeque<E> - Deque๋ ์ด์ค ์ข ๋ฃ ๋๊ธฐ์ด์ ๋ํ๋ ๋๋ค. ์ด๋ ์์ชฝ์์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๊ณ ๊ฐ์ ธ์ฌ ์ ์์์ ์๋ฏธํฉ๋๋ค. ๋ฐ๋ผ์ ์ด ํด๋์ค๋ FIFO(First In First Out) ๋ฐ LIFO(Last In First Out)์ ๋ ๊ฐ์ง ์๋ ๋ชจ๋๋ฅผ ๋ชจ๋ ์ง์ํฉ๋๋ค.
์ค์ ๋ก LIFO๊ฐ ์ ๋์ ์ผ๋ก ํ์ํ ๊ฒฝ์ฐ ConcurrentLinkedDeque๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ๋ ธ๋์ ์๋ฐฉํฅ์ฑ์ผ๋ก ์ธํด ์ด ํด๋์ค๋ ConcurrentLinkedQueue ์ ๋นํด ์ฑ๋ฅ์ด ์ ๋ฐ์ผ๋ก ๋จ์ด์ง๊ธฐ ๋๋ฌธ์ ๋๋ค .
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueExample {
public static void main(String[] args) {
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
Thread producer = new Thread(new Producer(queue));
Thread consumer = new Thread(new Consumer(queue));
producer.start();
consumer.start();
}
}
class Producer implements Runnable {
ConcurrentLinkedQueue<String> queue;
Producer(ConcurrentLinkedQueue<String> queue){
this.queue = queue;
}
public void run() {
System.out.println("Class for adding items to the queue");
try {
for (int i = 1; i < 5; i++) {
queue.add("Item #" + i);
System.out.println("Added: Item #" + i);
Thread.sleep(300);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
class Consumer implements Runnable {
ConcurrentLinkedQueue<String> queue;
Consumer(ConcurrentLinkedQueue<String> queue){
this.queue = queue;
}
public void run() {
String str;
System.out.println("Class for getting items from the queue");
for (int x = 0; x < 5; x++) {
while ((str = queue.poll()) != null) {
System.out.println("Pulled out: " + str);
}
try {
Thread.sleep(600);
} catch (InterruptedException ex) {
ex.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
}
์ฐจ๋จ ๋๊ธฐ์ด
![](https://cdn.codegym.cc/images/article/8c8152d6-ffe6-4165-879c-3bcae893696f/1080.jpeg)
BlockingQueue<E> ์ธํฐํ์ด์ค - ๋ฐ์ดํฐ๊ฐ ๋ง์ผ๋ฉด ConcurrentLinkedQueue ๋ก๋ ๋ถ์กฑํฉ๋๋ค.
์ค๋ ๋๊ฐ ์์ ์ ์ํํ์ง ๋ชปํ๋ฉด OutOfMemmoryException ์ ์ฝ๊ฒ ์ป์ ์ ์์ต๋๋ค . ์ด๋ฌํ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ๋๊ธฐ์ด ๋ฐ ์กฐ๊ฑด๋ถ ์ ๊ธ์ ์ฑ์ฐ๊ณ ์์ ํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ด ์๋ ์์ ์ ์ํ BlockingQueue๊ฐ ์์ต๋๋ค.
BlockingQueue๋ null ์์๋ฅผ ์ธ์ํ์ง ๋ชปํ๊ณ ์ด๋ฌํ ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ๊ฐ์ ธ์ค๋ ค๊ณ ํ ๋ NullPointerException์ ๋ฐ์์ํต๋๋ค. poll ๋ฉ์๋๋ ์ ํ ์๊ฐ ๋ด์ ๋๊ธฐ์ด์ ์์๊ฐ ๋ฐฐ์น๋์ง ์์ ๊ฒฝ์ฐ null ์์๋ฅผ ๋ฐํํฉ๋๋ค.
BlockingQueue<E> ๊ตฌํ
๊ฐ BlockingQueue ๊ตฌํ์ ์์ธํ ์ดํด๋ณด๊ฒ ์ต๋๋ค .
ArrayBlockingQueue<E>๋ ํด๋์ ๋ง ๋ฒํผ์ ๊ตฌ์ถ๋ ์ฐจ๋จ ๋๊ธฐ์ด ํด๋์ค์ ๋๋ค. ์ฌ๊ธฐ์์ ์๋ฌผ์ ์ "์ ์ง์ฑ"์ ๊ด๋ฆฌํ ๊ธฐํ๊ฐ ์์ต๋๋ค. fair=false(๊ธฐ๋ณธ๊ฐ)์ด๋ฉด ์ค๋ ๋ ์์๊ฐ ๋ณด์ฅ๋์ง ์์ต๋๋ค.
DelayQueue<E extends Delayed>๋ Delayed ์ธํฐํ์ด์ค ์ getDelay ๋ฉ์๋ ๋ฅผ ํตํด ๊ฐ ์์์ ์ ์๋ ํน์ ์ง์ฐ ์ดํ์๋ง ๋๊ธฐ์ด์์ ์์๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํ๋ ํด๋์ค์ ๋๋ค.
LinkedBlockingQueue<E> ๋ "2๊ฐ์ ์ ๊ธ ๋๊ธฐ์ด" ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๊ตฌํ๋ ์ฐ๊ฒฐ๋ ๋ ธ๋์ ์ฐจ๋จ ๋๊ธฐ์ด์ ๋๋ค. ์ฒซ ๋ฒ์งธ ์ ๊ธ์ ์ถ๊ฐ์ฉ์ด๊ณ ๋ ๋ฒ์งธ ์ ๊ธ์ ๋๊ธฐ์ด์์ ์์๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ ๊ฒ์ ๋๋ค. ์ ๊ธ์ผ๋ก ์ธํด ArrayBlockingQueue ์ ๋นํด์ด ํด๋์ค๋ ์ฑ๋ฅ์ด ๋์ง๋ง ๋ ๋ง์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค. ๋๊ธฐ์ด ํฌ๊ธฐ๋ ์์ฑ์๋ฅผ ํตํด ์ค์ ๋๋ฉฐ ๊ธฐ๋ณธ์ ์ผ๋ก Integer.MAX_VALUE์ ๊ฐ์ต๋๋ค.
PriorityBlockingQueue<E>๋ PriorityQueue ์ ๋ํ ๋ค์ค ์ค๋ ๋ ๋ํผ์ ๋๋ค. Comparator๋ ์์๊ฐ ์ถ๊ฐ๋๋ ๋ ผ๋ฆฌ๋ฅผ ๋ด๋นํฉ๋๋ค. ๊ฐ์ฅ ์์ ์์๊ฐ ๋จผ์ ๋์ต๋๋ค.
SynchronousQueue<E> - ๋๊ธฐ์ด์ FIFO(์ ์ ์ ์ถ) ์์น์ ๋ฐ๋ผ ์๋ํฉ๋๋ค. ๊ฐ ์ฝ์ ์์ ์ "์๋น์" ์ค๋ ๋๊ฐ ๋๊ธฐ์ด์์ ์์๋ฅผ ๊ฐ์ ธ์ฌ ๋๊น์ง "์์ฐ์" ์ค๋ ๋๋ฅผ ์ฐจ๋จํ๊ณ ๊ทธ ๋ฐ๋์ ๊ฒฝ์ฐ๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค. "์๋น์"๋ "์์ฐ์"๊ฐ ์์๋ฅผ ์ฝ์ ํ ๋๊น์ง ๋๊ธฐํฉ๋๋ค.
BlockingDeque<E>๋ ์๋ฐฉํฅ ์ฐจ๋จ ๋๊ธฐ์ด์ ๋ํ ์ถ๊ฐ ๋ฉ์๋๋ฅผ ์ค๋ช ํ๋ ์ธํฐํ์ด์ค์ ๋๋ค. ๋๊ธฐ์ด์ ์์ชฝ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ๊ณ ๊บผ๋ผ ์ ์์ต๋๋ค.
LinkedBlockingDeque<E>๋ ํ๋์ ์ ๊ธ์ด ์๋ ๊ฐ๋จํ ์๋ฐฉํฅ ๋ชฉ๋ก์ผ๋ก ๊ตฌํ๋ ์ฐ๊ฒฐ๋ ๋ ธ๋์ ์๋ฐฉํฅ ์ฐจ๋จ ๋๊ธฐ์ด์ ๋๋ค. ๋๊ธฐ์ด ํฌ๊ธฐ๋ ์์ฑ์๋ฅผ ํตํด ์ค์ ๋๋ฉฐ ๊ธฐ๋ณธ์ ์ผ๋ก Integer.MAX_VALUE์ ๊ฐ์ต๋๋ค.
TransferQueue<E> - ์์๊ฐ ๋๊ธฐ์ด์ ์ถ๊ฐ๋ ๋๋ค๋ฅธ ์๋น์ ์ค๋ ๋๊ฐ ๋๊ธฐ์ด์์ ์์๋ฅผ ๊ฐ์ ธ์ฌ ๋๊น์ง ์ฝ์ ํ๋ ์์ฐ์ ์ค๋ ๋๋ฅผ ์ฐจ๋จํ ์ ์๋ค๋ ์ ์์ ์ธํฐํ์ด์ค๊ฐ ํฅ๋ฏธ๋กญ์ต๋๋ค. ํน์ ์๊ฐ ์ ํ์ ๋ํ ํ์ธ์ ์ถ๊ฐํ๊ฑฐ๋ ๋๊ธฐ ์ค์ธ Consumer ์ ๋ํ ํ์ธ์ ์ค์ ํ ์๋ ์์ต๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก ๋น๋๊ธฐ ๋ฐ ๋๊ธฐ ๋ฉ์์ง๋ฅผ ์ง์ํ๋ ๋ฐ์ดํฐ ์ ์ก ๋ฉ์ปค๋์ฆ์ ์ป์ต๋๋ค.
LinkedTransferQueue<E>๋ Dual Queues with Slack ์๊ณ ๋ฆฌ์ฆ์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ TransferQueue์ ๊ตฌํ์ ๋๋ค์ ํด ์ํ์ผ ๋ CAS(์ ์ฐธ์กฐ) ๋ฐ ์ค๋ ๋ ํํน์ ๋ง์ด ์ฌ์ฉํฉ๋๋ค.