Java集合类框架源码分析 之 TransferQueue接口源码解析 【12】

先看类简介:

/**
 * 生产者可以等待消费者接收元素,当消息在应用传递时,生产者通过 transfer()等待消费者调用 take()/poll()来接收元素,很有用。在其他时候,通过put()可以不等待接收。
 * tryTransfer()/tryTransfer(Object) Non-blocking()/tryTransfer(Object,long,TimeUnit) time-out版本也可用。
 * 也可以通过hasWaitingConsumer()来查询是否有线程在等待元素,这与peek()操作相反。 
 * A {@link BlockingQueue} in which producers may wait for consumers
 * to receive elements.  A {@code TransferQueue} may be useful for
 * example in message passing applications in which producers
 * sometimes (using method {@link #transfer}) await receipt of
 * elements by consumers invoking {@code take} or {@code poll}, while
 * at other times enqueue elements (via method {@code put}) without
 * waiting for receipt.
 * {@linkplain #tryTransfer(Object) Non-blocking} and
 * {@linkplain #tryTransfer(Object,long,TimeUnit) time-out} versions of
 * {@code tryTransfer} are also available.
 * A {@code TransferQueue} may also be queried, via {@link
 * #hasWaitingConsumer}, whether there are any threads waiting for
 * items, which is a converse analogy to a {@code peek} operation.
 *
 * 就像其他的阻塞队列,TransferQueue也可能有容量限制.如果这样,尝试的传输操作可能首先阻塞对可用空间的等待,并且/或随后阻塞对消费者接收的等待。
 * 注意,在一个容量为零的队列中,例如{@link SynchronousQueue}, {@code put}和{@code transfer}实际上是同义的。 
 * <p>Like other blocking queues, a {@code TransferQueue} may be
 * capacity bounded.  If so, an attempted transfer operation may
 * initially block waiting for available space, and/or subsequently
 * block waiting for reception by a consumer.  Note that in a queue
 * with zero capacity, such as {@link SynchronousQueue}, {@code put}
 * and {@code transfer} are effectively synonymous.
 *
 * <p>This interface is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @since 1.7
 * @author Doug Lea
 * @param <E> the type of elements held in this collection
 */

从类简介中可知两点:

1、生产者通过 transfer()等待消费者调用 take()/poll()来接收元素,如果消费者没有获取元素,那么当此入队操作就是一次失败的入队操作。

2、SynchronousQueue 是一个容量为0的BlockingQueue队列,并非是TransferQueue队列,但它的put()和TransferQueue中的transfer()操作是同义的。

看类源码实现:

public interface TransferQueue<E> extends BlockingQueue<E> {
    /**
     * 等待 consumer之后,立马开始传输一个文件。
     * Transfers the element to a waiting consumer immediately, if possible.
     *
     * 更严格来说,如果存在一个用户通过take()或者定时poll(),已经在等待接收,可以立马进行元素传输。否认元素没有入队列,并返回false.
     * 如果被添加的元素,阻止其被添加到队列中,返回false。
     * <p>More precisely, transfers the specified element immediately
     * if there exists a consumer already waiting to receive it (in
     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
     * otherwise returning {@code false} without enqueuing the element.
     *
     * @param e the element to transfer
     * @return {@code true} if the element was transferred, else
     *         {@code false}
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this queue
     */
    boolean tryTransfer(E e);

    /**
     * 如果需要的话,给消费者传递一个元素。
     * Transfers the element to a consumer, waiting if necessary to do so.
     *
     * 更严格来说,如果存在一个用户通过take()或者定时poll(),已经在等待接收,可以立马进行元素传输。否则一直等待直到元素被消费者接收。
     * <p>More precisely, transfers the specified element immediately
     * if there exists a consumer already waiting to receive it (in
     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
     * else waits until the element is received by a consumer.
     *
     * @param e the element to transfer
     * @throws InterruptedException if interrupted while waiting,
     *         in which case the element is not left enqueued
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this queue
     */
    void transfer(E e) throws InterruptedException;

    /**
     * 在超时之前,如果可能的话,给消费者传递一个元素
     * Transfers the element to a consumer if it is possible to do so
     * before the timeout elapses.
     *
     * 在元素被传递之前,等待指定的时间,超时则返回false。
     * <p>More precisely, transfers the specified element immediately
     * if there exists a consumer already waiting to receive it (in
     * {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
     * else waits until the element is received by a consumer,
     * returning {@code false} if the specified wait time elapses
     * before the element can be transferred.
     *
     * @param e the element to transfer
     * @param timeout how long to wait before giving up, in units of
     *        {@code unit}
     * 等待时间的单位
     * @param unit a {@code TimeUnit} determining how to interpret the
     *        {@code timeout} parameter
     * 成功的话返回true,否则返回false,此时元素没有离开队列。
     * @return {@code true} if successful, or {@code false} if
     *         the specified waiting time elapses before completion,
     *         in which case the element is not left enqueued
     * 等待的时候被打断,抛出 InterruptedException异常,此时元素没有离开队列。
     * @throws InterruptedException if interrupted while waiting,
     *         in which case the element is not left enqueued
     * 如果指定的元素拒绝被插入队列,抛出 ClassCastException异常。
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null
     * 如果指定元素的部分属性,拒绝被插入队列,抛出 IllegalArgumentException异常。
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this queue
     */
    boolean tryTransfer(E e, long timeout, TimeUnit unit)
        throws InterruptedException;

    /**
     * 如果至少有一个消费者通过take()/定时poll()在等待接收元素,返回事务瞬时状态值(true)
     * Returns {@code true} if there is at least one consumer waiting
     * to receive an element via {@link #take} or
     * timed {@link #poll(long,TimeUnit) poll}.
     * The return value represents a momentary state of affairs.
     *
     * @return {@code true} if there is at least one waiting consumer
     */
    boolean hasWaitingConsumer();

    /**
     * 返回正在通过take/定时poll接收元素的消费者预估数量。返回值是瞬时状态的预估值,如果消费者完成或放弃等待,该值可能不准确。
     * 该值作为监控和探索可能比较有用,但是不能作为同步控制。该方法实现,明显比 hasWaitingConsumer()慢。
     * Returns an estimate of the number of consumers waiting to
     * receive elements via {@link #take} or timed
     * {@link #poll(long,TimeUnit) poll}.  The return value is an
     * approximation of a momentary state of affairs, that may be
     * inaccurate if consumers have completed or given up waiting.
     * The value may be useful for monitoring and heuristics, but
     * not for synchronization control.  Implementations of this
     * method are likely to be noticeably slower than those for
     * {@link #hasWaitingConsumer}.
     *
     * @return the number of consumers waiting to receive elements
     */
    int getWaitingConsumerCount();
}

 tryTransfer():会立马返回结果,如果成功,就返回true,失败返回false;

 transfer():会等待元素被消费者取走

tryTransfer(E e, long timeout, TimeUnit unit):在指定的时间内等待元素被消费者接收,否则返回false;

getWaitingConsumerCount():该方法只是一个大概的估计值,作为监控比较有用,但是不能作为同步的依据,并且会比hasWaitingConsumer()慢。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
4S店客户管理小程序-毕业设计,基于微信小程序+SSM+MySql开发,源码+数据库+论文答辩+毕业论文+视频演示 社会的发展和科学技术的进步,互联网技术越来越受欢迎。手机也逐渐受到广大人民群众的喜爱,也逐渐进入了每个用户的使用。手机具有便利性,速度快,效率高,成本低等优点。 因此,构建符合自己要求的操作系统是非常有意义的。 本文从管理员、用户的功能要求出发,4S店客户管理系统中的功能模块主要是实现管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理,用户客户端:首页、车展、新闻头条、我的。门店客户端:首页、车展、新闻头条、我的经过认真细致的研究,精心准备和规划,最后测试成功,系统可以正常使用。分析功能调整与4S店客户管理系统实现的实际需求相结合,讨论了微信开发者技术与后台结合java语言和MySQL数据库开发4S店客户管理系统的使用。 关键字:4S店客户管理系统小程序 微信开发者 Java技术 MySQL数据库 软件的功能: 1、开发实现4S店客户管理系统的整个系统程序; 2、管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理等。 3、用户客户端:首页、车展、新闻头条、我的 4、门店客户端:首页、车展、新闻头条、我的等相应操作; 5、基础数据管理:实现系统基本信息的添加、修改及删除等操作,并且根据需求进行交流信息的查看及回复相应操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值