转自:http://blog.csdn.net/aitangyong/article/details/46472643

JDK7对JDK5中的J.U.C并发工具进行了增强,其中之一就是新增了TransferQueue。Java并发相关的JSR规范,可以查看Doug Lea维护的blog。现在简单介绍下这个类的使用方式。

  1. public interface TransferQueue<E> extends BlockingQueue<E>
  2. {
  3. /**
  4. * Transfers the element to a waiting consumer immediately, if possible.
  5. *
  6. * <p>More precisely, transfers the specified element immediately
  7. * if there exists a consumer already waiting to receive it (in
  8. * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
  9. * otherwise returning {@code false} without enqueuing the element.
  10. *
  11. * @param e the element to transfer
  12. * @return {@code true} if the element was transferred, else
  13. *         {@code false}
  14. * @throws ClassCastException if the class of the specified element
  15. *         prevents it from being added to this queue
  16. * @throws NullPointerException if the specified element is null
  17. * @throws IllegalArgumentException if some property of the specified
  18. *         element prevents it from being added to this queue
  19. */
  20. boolean tryTransfer(E e);
  21. /**
  22. * Transfers the element to a consumer, waiting if necessary to do so.
  23. *
  24. * <p>More precisely, transfers the specified element immediately
  25. * if there exists a consumer already waiting to receive it (in
  26. * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
  27. * else waits until the element is received by a consumer.
  28. *
  29. * @param e the element to transfer
  30. * @throws InterruptedException if interrupted while waiting,
  31. *         in which case the element is not left enqueued
  32. * @throws ClassCastException if the class of the specified element
  33. *         prevents it from being added to this queue
  34. * @throws NullPointerException if the specified element is null
  35. * @throws IllegalArgumentException if some property of the specified
  36. *         element prevents it from being added to this queue
  37. */
  38. void transfer(E e) throws InterruptedException;
  39. /**
  40. * Transfers the element to a consumer if it is possible to do so
  41. * before the timeout elapses.
  42. *
  43. * <p>More precisely, transfers the specified element immediately
  44. * if there exists a consumer already waiting to receive it (in
  45. * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
  46. * else waits until the element is received by a consumer,
  47. * returning {@code false} if the specified wait time elapses
  48. * before the element can be transferred.
  49. *
  50. * @param e the element to transfer
  51. * @param timeout how long to wait before giving up, in units of
  52. *        {@code unit}
  53. * @param unit a {@code TimeUnit} determining how to interpret the
  54. *        {@code timeout} parameter
  55. * @return {@code true} if successful, or {@code false} if
  56. *         the specified waiting time elapses before completion,
  57. *         in which case the element is not left enqueued
  58. * @throws InterruptedException if interrupted while waiting,
  59. *         in which case the element is not left enqueued
  60. * @throws ClassCastException if the class of the specified element
  61. *         prevents it from being added to this queue
  62. * @throws NullPointerException if the specified element is null
  63. * @throws IllegalArgumentException if some property of the specified
  64. *         element prevents it from being added to this queue
  65. */
  66. boolean tryTransfer(E e, long timeout, TimeUnit unit)
  67. throws InterruptedException;
  68. /**
  69. * Returns {@code true} if there is at least one consumer waiting
  70. * to receive an element via {@link #take} or
  71. * timed {@link #poll(long,TimeUnit) poll}.
  72. * The return value represents a momentary state of affairs.
  73. *
  74. * @return {@code true} if there is at least one waiting consumer
  75. */
  76. boolean hasWaitingConsumer();
  77. /**
  78. * Returns an estimate of the number of consumers waiting to
  79. * receive elements via {@link #take} or timed
  80. * {@link #poll(long,TimeUnit) poll}.  The return value is an
  81. * approximation of a momentary state of affairs, that may be
  82. * inaccurate if consumers have completed or given up waiting.
  83. * The value may be useful for monitoring and heuristics, but
  84. * not for synchronization control.  Implementations of this
  85. * method are likely to be noticeably slower than those for
  86. * {@link #hasWaitingConsumer}.
  87. *
  88. * @return the number of consumers waiting to receive elements
  89. */
  90. int getWaitingConsumerCount();
  91. }

可以看到TransferQueue同时也是一个阻塞队列,它具备阻塞队列的所有特性,主要介绍下上面5个新增API的作用。

1.transfer(E e)若当前存在一个正在等待获取的消费者线程,即立刻将e移交之;否则将元素e插入到队列尾部,并且当前线程进入阻塞状态,直到有消费者线程取走该元素。

  1. public class TransferQueueDemo {
  2. private static TransferQueue<String> queue = new LinkedTransferQueue<String>();
  3. public static void main(String[] args) throws Exception {
  4. new Productor(1).start();
  5. Thread.sleep(100);
  6. System.out.println("over.size=" + queue.size());
  7. }
  8. static class Productor extends Thread {
  9. private int id;
  10. public Productor(int id) {
  11. this.id = id;
  12. }
  13. @Override
  14. public void run() {
  15. try {
  16. String result = "id=" + this.id;
  17. System.out.println("begin to produce." + result);
  18. queue.transfer(result);
  19. System.out.println("success to produce." + result);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. }

可以看到生产者线程会阻塞,因为调用transfer()的时候并没有消费者在等待获取数据。队列长度变成了1,说明元素e没有移交成功的时候,会被插入到阻塞队列的尾部。

2.tryTransfer(E e)若当前存在一个正在等待获取的消费者线程,则该方法会即刻转移e,并返回true;若不存在则返回false,但是并不会将e插入到队列中。这个方法不会阻塞当前线程,要么快速返回true,要么快速返回false。

3.hasWaitingConsumer()和getWaitingConsumerCount()用来判断当前正在等待消费的消费者线程个数。

4.tryTransfer(E e, long timeout, TimeUnit unit) 若当前存在一个正在等待获取的消费者线程,会立即传输给它; 否则将元素e插入到队列尾部,并且等待被消费者线程获取消费掉。若在指定的时间内元素e无法被消费者线程获取,则返回false,同时该元素从队列中移除。

  1. public class TransferQueueDemo {
  2. private static TransferQueue<String> queue = new LinkedTransferQueue<String>();
  3. public static void main(String[] args) throws Exception {
  4. new Productor(1).start();
  5. Thread.sleep(100);
  6. System.out.println("over.size=" + queue.size());//1
  7. Thread.sleep(1500);
  8. System.out.println("over.size=" + queue.size());//0
  9. }
  10. static class Productor extends Thread {
  11. private int id;
  12. public Productor(int id) {
  13. this.id = id;
  14. }
  15. @Override
  16. public void run() {
  17. try {
  18. String result = "id=" + this.id;
  19. System.out.println("begin to produce." + result);
  20. queue.tryTransfer(result, 1, TimeUnit.SECONDS);
  21. System.out.println("success to produce." + result);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }

第一次还没到指定的时间,元素被插入到队列中了,所有队列长度是1;第二次指定的时间片耗尽,元素从队列中移除了,所以队列长度是0。

这篇文章中讲了SynchronousQueue的使用方式,可以看到TransferQueue也具有SynchronousQueue的所有功能,但是TransferQueue的功能更强大。这篇文章中提到了这2个API的区别:

  1. TransferQueue is more generic and useful than SynchronousQueue however as it allows you to flexibly decide whether to use normal BlockingQueue
  2. semantics or a guaranteed hand-off. In the case where items are already in the queue, calling transfer will guarantee that all existing queue
  3. items will be processed before the transferred item.
  4. SynchronousQueue implementation uses dual queues (for waiting producers and waiting consumers) and protects both queues with a single lock. The
  5. LinkedTransferQueue implementation uses CAS operations to form a nonblocking implementation and that is at the heart of avoiding serialization
  6. bottlenecks.

【Java并发编程】4、JDK7中TransferQueue的使用以及TransferQueue与SynchronousQueue的差别的更多相关文章

  1. Java并发编程(3) JUC中的锁

    一 前言 前面已经说到JUC中的锁主要是基于AQS实现,而AQS(AQS的内部结构 .AQS的设计与实现)在前面已经简单介绍过了.今天记录下JUC包下的锁是怎么基于AQS上实现的 二 同步锁 同步锁不 ...

  2. Java并发编程:JDK中的阻塞队列

    上次我们讲了一些常用的4个阻塞队列,但是在JDK中还提供了其他的一些阻塞队列.这篇文章将全面介绍一下JDK中的所有阻塞队列,并比较他们的区别. JDK7提供了7个阻塞队列.分别是 ArrayBlock ...

  3. 整理一下《java并发编程实战》中的知识点

    分工.同步.互斥的历史由来 分工:单道.多道.分时 同步:线程通信(组织编排任务) 互斥:因(多线程访问共享资源)果(串行化共享资源的访问) 1切都是为了提高性能 2.可见性.原子性.有序性 可见性: ...

  4. Java并发编程面试题 Top 50 整理版

    本文在 Java线程面试题 Top 50的基础上,对部分答案进行进行了整理和补充,问题答案主要来自<Java编程思想(第四版)>,<Java并发编程实战>和一些优秀的博客,当然 ...

  5. Java并发编程73道面试题及答案 —— 面试稳了

    今天主要整理一下 Java 并发编程在面试中的常见问题,希望对需要的读者有用. 1.在java中守护线程和本地线程区别? java中的线程分为两种:守护线程(Daemon)和用户线程(User). 任 ...

  6. Java并发编程学习路线(转)

    以前特地学过并发编程,但是没怎么学进去,不太喜欢.最近发现,作为一个资深工程师,却没有完整深入系统的学习过,而反是现在的BAT大并发是必须的,感觉甚是惭愧. 故找了一片学习文章,如下,准备集中一段时间 ...

  7. Java并发编程学习路线

    一年前由于工作需要从微软技术栈入坑Java,并陆陆续续做了一个Java后台项目,目前在搞Scala+Java混合的后台开发,一直觉得并发编程是所有后台工程师的基本功,所以也学习了小一年Java的并发工 ...

  8. Java 并发编程-不懂原理多吃亏(送书福利)

    作者 | 加多 关注阿里巴巴云原生公众号,后台回复关键字"并发",即可参与送书抽奖!** 导读:并发编程与 Java 中其他知识点相比较而言学习门槛较高,从而导致很多人望而却步.但 ...

  9. java并发编程之美-阅读记录1

    1.1什么是线程? 在理解线程之前先要明白什么是进程,因为线程是进程中的一个实体.(线程是不会独立存在的) 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,线程则是进程中的 ...

  10. Java并发编程(您不知道的线程池操作), 最受欢迎的 8 位 Java 大师,Java并发包中的同步队列SynchronousQueue实现原理

    Java_并发编程培训 java并发程序设计教程 JUC Exchanger 一.概述 Exchanger 可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchan ...

随机推荐

  1. WPF自定义Window窗体样式

    资源文件代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation ...

  2. .NET MVC 学习笔记(四)— 基于Bootstarp自定义弹出框

    .NET MVC 学习笔记(四)—— 基于Bootstarp自定义弹出框 转载自:https://www.cnblogs.com/nele/p/5327380.html (function ($) { ...

  3. Ef-Code-First 使用实体类映射出数据库

    最近面试时很多面试官都问到了EF框架 好记性不如烂笔头 赶紧记下来 code-first是EF框架中的一种,是使用实体类来进行数据库表的映射,所以实体类中的字段要规范(我认为) 比如: 如果有外键的话 ...

  4. MVC+Nhibernate+spring.net(二)

    在上一篇文章中我们已经把数据查了出来,现在我们来完善一下:前台使用easyui 首先我们将NHelper类完善一下 public class EmpDal { public IList<Emp& ...

  5. 【Atcoder】 AGC032赛后总结

    比赛前 emmm,今天是场AGC,想起上次我的惨痛经历(B都不会),这次估计要凉,可能A都不会Flag1 比赛中 看场看了波\(A\),咦,这不是很呆的题目吗?倒着扫一遍就好了. 然后切了就开始看B, ...

  6. JQuery Mobile - 自定义图标!

    环境 jquery1.9.1.js jquery mobile 1.4.2版 图标制作 18x18px 底透明的png图标 使用图标,这个过程开始怎么弄都不行,只有个灰色的圆出现,不见图标:或者出现图 ...

  7. [学习笔记]K-D Tree

    以前其实学过的但是不会拍扁重构--所以这几天学了一下 \(K-D\ Tree\) 的正确打开姿势. \(K\) 维 \(K-D\ Tree\) 的单次操作最坏时间复杂度为 \(O(k\times n^ ...

  8. Jmeter 结构、原理介绍

    Jmeter结构.原理介绍 一.Jmeter 简介 1.是基于java语言的开源的应用软件. 2.可以进行接口测试.性能测试.接口及性能的自动化测试. 二.Jmeter体系结构 元件:可以理解为每一个 ...

  9. 解决 在 WINDOWS 下 同时安装 python2 python3 后 pip 错误

    再之前同时安装 python 后 只需把环境变量PATH 里面改为 PATH=C:\Python36-32\Scripts\;C:\Python36-32\;C:\Python27\;C:\Pytho ...

  10. SqlServer 查看被锁的表和解除被锁的表

    查看被锁的表 1 2 select   request_session_id   spid,OBJECT_NAME(resource_associated_entity_id) tableName   ...