介绍
Java 6的并发编程包中的SynchronousQueue是一个没有数据缓冲的BlockingQueue(队列只能存储一个元素),生产者线程对其的插入操作put必须等待消费者的移除操作take,反过来也一样,消费者移除数据操作必须等待生产者的插入。
不像ArrayBlockingQueue或LinkedListBlockingQueue,SynchronousQueue内部并没有数据缓存空间,你不能调用peek()方法来看队列中是否有数据元素,因为数据元素只有当你试着取走的时候才可能存在,不取走而只想偷窥一下是不行的,当然遍历这个队列的操作也是不允许的。队列头元素是第一个排队要插入数据的线程,而不是要交换的数据。数据是在配对的生产者和消费者线程之间直接传递的,并不会将数据缓冲数据到队列中。可以这样来理解:生产者和消费者互相等待对方,握手,然后一起离开。
SynchronousQueue的一个使用场景是在线程池里。Executors.newCachedThreadPool()就使用了SynchronousQueue,这个线程池根据需要(新任务到来时)创建新的线程,如果有空闲线程则会重复使用,线程空闲了60秒后会被回收。
- <strong style="color: rgb(0, 102, 0);"> </strong>
- ExecutorService es1 = Executors.newCachedThreadPool();
java.util.concurrent.Executors.newCachedThreadPool实现:
- public static ExecutorService newCachedThreadPool() {
- return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
- }
(此部分内容摘抄至:http://ifeve.com/java-synchronousqueue/)
接下来研究一下SynchronousQueue的使用:
SynchronousQueue创建:
-
-
- SynchronousQueue<Integer> sc = new SynchronousQueue<>();
由于
SynchronousQueue是没有缓冲区的,所以如下方法不可用:
- sc.peek();
- sc.clear();
- sc.contains(1);
- sc.containsAll(new ArrayList<Integer>());
- sc.isEmpty();
- sc.size();
- sc.toArray();
- Integer [] in=new Integer[]{new Integer(2)};
- sc.toArray(in);
- sc.removeAll(new ArrayList<Integer>());
- sc.retainAll(new ArrayList<Integer>());
- sc.remove("a");
- sc.peek();
由于SynchronousQueue 队列中最多只有一个元素,所以这些方法是没有意义的,所以在对方法的实现体中阉割掉了。
SynchronousQueue 获取元素:
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- SynchronousQueue<Integer> sc = new SynchronousQueue<>();
-
- sc.poll();
- sc.poll(5,TimeUnit.SECONDS);
- }
- }
SynchronousQueue
存入元素:
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- SynchronousQueue<Integer> sc = new SynchronousQueue<>();
-
- sc.offer(2);
- sc.offer(2, 5, TimeUnit.SECONDS);
- }
- }
总结:take和put是阻塞的获取和存储元素的方法,poll和offer是不阻塞的获取元素和存储元素的方法,并且poll和offer可以指定超时时间。
以上是基础Api的讲解,但是并不是实际的用法。接下来看一下使用的Demo:
- public class SynchronousQueueMain {
- public static void main(String[] args) throws Exception {
-
-
- SynchronousQueue<Integer> sc = new SynchronousQueue<>();
- new Thread(() -> {
- while (true) {
- try {
- sc.put(new Random().nextInt(50));
-
-
-
-
-
- System.out.println("添加操作运行完毕...");
- Thread.sleep(1000);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- }).start();
- new Thread(() -> {
- while (true) {
- try {
- System.out.println("-----------------> sc.take: " + sc.take());
- System.out.println("-----------------> 获取操作运行完毕...");
- Thread.sleep(1000);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- }).start();
- }
- }
take 、poll和put、offer可以组成使用,可以根据实际业务需求选择!