JDK中有直接可以使用的阻塞队列

是的,Java标准库(JDK)中提供了多个阻塞队列,可以直接使用。这些阻塞队列位于java.util.concurrent包中。阻塞队列是一种支持在某些操作无法立即完成时等待的队列,例如在队列为空时执行的take操作,或者在队列已满时执行的put操作。

以下是JDK中几种常见的阻塞队列及其特点:

1. ArrayBlockingQueue

ArrayBlockingQueue是一个基于数组的有界阻塞队列。其特点如下:

  • 固定大小(在创建时指定)。
  • FIFO(先进先出)顺序。
  • 可以指定公平性策略,以确保等待的线程按FIFO顺序访问队列。
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ArrayBlockingQueueExample {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);

        // Producer
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    queue.put(i);
                    System.out.println("Produced: " + i);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();

        // Consumer
        new Thread(() -> {
            try {
                while (true) {
                    Integer item = queue.take();
                    System.out.println("Consumed: " + item);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
}

2. LinkedBlockingQueue

LinkedBlockingQueue是一个基于链表的可选有界阻塞队列。其特点如下:

  • 可以指定大小,也可以默认大小(默认是Integer.MAX_VALUE)。
  • FIFO顺序。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class LinkedBlockingQueueExample {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10);

        // Producer
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    queue.put(i);
                    System.out.println("Produced: " + i);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();

        // Consumer
        new Thread(() -> {
            try {
                while (true) {
                    Integer item = queue.take();
                    System.out.println("Consumed: " + item);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
}

3. PriorityBlockingQueue

PriorityBlockingQueue是一个支持优先级的无界阻塞队列。其特点如下:

  • 元素按照其自然顺序或者通过提供的比较器排序。
  • 没有容量限制(无界)。
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class PriorityBlockingQueueExample {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new PriorityBlockingQueue<>();

        // Producer
        new Thread(() -> {
            try {
                for (int i = 10; i > 0; i--) {
                    queue.put(i);
                    System.out.println("Produced: " + i);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();

        // Consumer
        new Thread(() -> {
            try {
                while (true) {
                    Integer item = queue.take();
                    System.out.println("Consumed: " + item);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
}

4. SynchronousQueue

SynchronousQueue是一个不存储元素的阻塞队列。每个插入操作必须等待相应的移除操作,反之亦然。其特点如下:

  • 不存储元素。
  • 每个put操作必须等待一个take操作,反之亦然。
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.BlockingQueue;

public class SynchronousQueueExample {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new SynchronousQueue<>();

        // Producer
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    queue.put(i);
                    System.out.println("Produced: " + i);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();

        // Consumer
        new Thread(() -> {
            try {
                while (true) {
                    Integer item = queue.take();
                    System.out.println("Consumed: " + item);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
}

总结

Java的java.util.concurrent包中提供了多种阻塞队列,包括ArrayBlockingQueueLinkedBlockingQueuePriorityBlockingQueueSynchronousQueue,可以根据不同的使用场景选择合适的阻塞队列。这些队列实现了流式处理,支持多线程环境下的高效数据传输和同步。

posted @ 2024-07-04 19:48  gongchengship  阅读(3)  评论(0编辑  收藏  举报