`

Delayed interface and Delay Queue

阅读更多

/**

 * A standard implementation for Delayed interface 

 *

 * A Delayed instance means something which has a time attribute. Generally means a thread     

 * will start up this time later.

 *

 * And obviously, Delayed instances can be compared with each other – which one sooner,

 * which one is later. So, Delayed extends Comparable<Delayed>

 *

 * Delayed instances can be put into DelayQueue.

 * DelayQueue is declared in the following way:

 * DelayQueue<E extends Delayed>

 *

 * DelayQueue has 2 characters:

 * 1) All Delayed instances are automatically sorted – the most urgent Delayed instance will  

 * be returned when take() method is called on the DelayQueue

 * 2) Only Expired Delayed instances are visible in the DelayQueue – if no expired instance

 * – take() method will block.

 *

 * From this point of view, DelayQueue is an advanced unbounded BlockingQueue

 *

 */

class DelayedTask implements Runnable, Delayed

{

      private final int delta;

      private final long trigger;

 

      public DelayedTask(int delayInMilliseconds)

      {

            delta = delayInMilliseconds;

            trigger = System.nanoTime() + NANOSECONDS.convert(delta, MILLISECONDS);

      }

 

 

//--These 2 methods are required by Delayed interface, you must implement them

//--And exactly in this way----------------------------------------------------

//--Delayed interface extends Comparable<Delayed>------------------------------

      public long getDelay(TimeUnit unit)

      {

            return unit.convert(trigger - System.nanoTime(), NANOSECONDS);

      }

 

      public int compareTo(Delayed arg)

      {

            DelayedTask that = (DelayedTask) arg;

            if (trigger < that.trigger) return -1;

            if (trigger > that.trigger) return 1;

            return 0;

      }

//-----------------------------------------------------------------------------

}

 

Another thing to mention is TimeUnit convert.

 

NANOSECONDS.convert(delta, MILLISECONDS);

This line means delta is a value in MILLISECONDS, and I want to convert it into NANOSECONDS.

 

unit.convert(trigger - System.nanoTime(), NANOSECONDS);

This line means trigger - System.nanoTime() is a value in NANOSECONDS, and I want to convert it into unit value.

 

 

A DelayQueue sample code is attached.

分享到:
评论

相关推荐

    Error performance of transmit beamforming with delayed and limited feedback

    ### 反馈延迟对传输波束成形性能的影响 #### 摘要解析与扩展 在现代通信系统中,特别是多天线系统中,传输波束成形(Transmit Beamforming, TB)作为一种有效的对抗多径衰落的技术,其性能受到了广泛的关注。...

    rabbitmq_delayed_3.6.x延迟插件.rar

    rabbitmqadmin declare queue name=my_delayed_queue rabbitmqadmin declare binding source=my_delayed_exchange destination=my_delayed_queue routing_key=* ``` 4. 发布延迟消息:发布消息时,添加`x-delay`...

    rabbitmq_delayed_message_exchange3.7

    channel.queue_bind(queue='delayed_queue', exchange='delayed_exchange', routing_key='') # 发布延迟消息 delay_time = 60 * 1000 # 1分钟延迟 props = pika.BasicProperties(delivery_mode=2, headers={'x-...

    RabbitMQ消息插件rabbitmq_delayed_message_exchange3.7

    rabbitmqadmin declare binding source=delayed_exchange destination=my_queue routing_key=my_routing_key ``` 4. **消费消息**:消费者可以从`my_queue`队列中正常接收延迟后到达的消息。 **应用场景** 1. **...

    demo_rabbitmq_x-delay.zip

    channel.queue_bind(exchange='delayed_exchange', queue=queue_name, routing_key='') def callback(ch, method, properties, body): print(f'Received message: {body} after delay') channel.basic_consume...

    rabbitmq_delayed_message_exchange-20171215-3.6.x.ez.zip

    在这段代码中,`my_delayed_exchange`是我们声明的延时交换机,`x-delay`属性设置为30000毫秒(即30秒)。 需要注意的是,延时交换机不支持所有类型的绑定,目前仅支持Direct和Topic类型的路由键。此外,由于延迟...

    rabbitmq_delayed_message_exchange-3.9.0.ez

    2. X_DELAYED_TYPE属性:在创建延迟交换机时,需要设置`x-delayed-type`参数为普通交换机类型(如`direct`、`topic`等),这使得延迟交换机具备了普通交换机的功能,同时添加了延迟特性。 二、RabbitMQ 3.9.0版本新...

    rabbitmq_delayed_message_exchange-20171215-3.6.x.zip

    在这个压缩包文件"rabbitmq_delayed_message_exchange-20171215-3.6.x.zip"中,我们关注的核心是延迟消息交换(Delayed Message Exchange)功能,这是RabbitMQ在3.6.x版本中引入的一个重要特性。该功能允许用户设置...

    rabbitmq_delayed_message_exchange-3.8.0.ez RabittMq及延时队列插件

    channel.queue_bind(queue=queue_name, exchange='delayed_exchange', routing_key='') ``` **应用场景** 1. **订单超时处理**:在电商系统中,可以使用延时队列来处理未支付的订单,比如在30分钟后自动取消未支付...

    rabbitmq-server-windows-3.8.9,内含延迟插件(rabbitmq_delayed_message_exchange-3.8.0.ez)

    channel.basic_publish(exchange='delayed_exchange', routing_key='queue_name', body='Hello, World!', properties=props) print(" [x] Sent 'Hello, World!'") connection.close() ``` 这里的“x-delay”头就是...

    rabbitmq_delayed_message_exchange-20171201-3.7.x.ez 插件

    在这个场景中,我们关注的是`rabbitmq_delayed_message_exchange-20171201-3.7.x.ez`插件,这是一个用于RabbitMQ的特殊交换机类型,它允许我们延迟消息的投递,即在特定时间后才将消息发送到队列。 **延迟消息...

    df_deriv.rar_delay and multiply_deriv_empty

    xx state variable and delayed state variables columnwise par list of parameter values nx empty or list of requested state-derivatives (numbers of delay or zero) np empty or list of requested ...

    rabbitmq_delayed_message_exchange-3.8.0.ez.zip

    延时队列的实现原理主要依赖于两个核心概念:交换机(Exchange)和队列(Queue)。交换机负责根据预设的路由规则将消息分发到相应的队列。在这个插件中,我们可能会使用一种特殊的交换机类型,如"x-delayed-message...

    rabbitmq_delayed_message_exchange-3.8.0 延迟队列插件

    RabbitMQ延迟队列插件,即rabbitmq_delayed_message_exchange-3.8.0,是一个针对RabbitMQ消息代理的扩展,旨在提供一种机制,使得消息能够在特定延迟后才被投递到相应的队列。这个功能在很多业务场景中非常有用,...

    rabbitmq-delayed-message-exchange-20171215-3.6.x.zip

    rabbitmq_delayed_message_exchange-20171215-3.6.x.zip 是一个为RabbitMQ设计的插件压缩包,旨在为RabbitMQ添加延迟消息交换功能; 以下是几个典型的使用场景示例: 订单超时处理: 当用户下单后未及时支付时,...

    rabbitmq_delayed_message_exchange-3.8.0.zip

    安装一个插件即可:https://www.rabbitmq.com/community-plugins.html ,下载rabbitmq_delayed_message_exchange插件,然后解压放置到RabbitMQ的插件目录...rabbitmq-plugins enable rabbitmq_delayed_message_exchange

    Distributed Optimal Consensus Control for Multiagent Systems With Input Delay

    delay-free system. Two delicate performance index functions are defined for these two systems. It is shown that the performance index functions are equivalent and the optimal consensus control problem...

    rabbitmq_delayed_message_exchange-3.9.0.zip

    截止2021.08.19日适配官网最新版rabbitmq3.9.3的消息延时队列插件,包内含有rabbitmq_delayed_message_exchange-3.9.0.ez格式、zip格式、tar.gz格式安装包任你选择~~

Global site tag (gtag.js) - Google Analytics