DelayQueue的用法

本文介绍了一个使用Java的DelayQueue实现的简单延迟任务处理机制。通过自定义DelayItem类实现Delayed接口,可以设定任务的延迟时间,并利用DelayQueue自动管理这些任务。当延迟时间到达后,任务会被自动取出并进行处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.fit.test;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayTest {

	static class DelayItem implements Delayed {

		public String	id;
		public long		daltime;
		public long		delay;
		public long		time;

		public DelayItem ( String id ) {
			this.id = id;
		}

		public DelayItem ( String id, long daltime, long delay ) {
			this.id = id;
			this.daltime = daltime;
			this.delay = delay;
			this.time = this.daltime + this.delay;
		}

		/**
		 * 预期时间在前的大:return 1
		 */
		@Override
		public int compareTo( Delayed o ) {
			long result = ( (DelayAlarm) o ).getTime() - this.getTime();
			if ( result < 0 )
				return 1;
			if ( result > 0 )
				return -1;
			return 0;
		}

		/**
		 * 预期时间-当前时间
		 */
		@Override
		public long getDelay( TimeUnit unit ) {
			return unit.convert( time * 1000L - System.currentTimeMillis(), TimeUnit.MILLISECONDS );
		}

		/**
		 * 根据流水号删除
		 */
		@Override
		public boolean equals( Object o ) {
			if ( o == null )
				return false;
			DelayAlarm a = (DelayItem ) o;
			if ( null == this.getId() || null == a.getId() )
				return false;
			return this.id.equals( a.getId() );
		}

		public String getId() {
			return id;
		}

		public long getDaltime() {
			return daltime;
		}

		public long getDelay() {
			return delay;
		}

		public long getTime() {
			return time;
		}

		public String toString() {
			return "{id:" + id + ", daltime:" + daltime + ", delay:" + delay + " , time:" + time + "}";
		}
	}

	//缓存队列
	public static DelayQueue<DelayItem>	queue	= new DelayQueue<DelayItem>();
	//
	public static volatile boolean			stop;

	public static void push( DelayItem a ) {
		if ( !queue.offer( a ) ) {
			System.out.println( "/- push error." );
		}
	}

	static class Consumer extends Thread {

		public void run() {
			while ( !stop ) {
				try {
					DelayItem a = queue.take();
					System.out.println( System.currentTimeMillis() / 1000L + "----" + a );
				} catch ( InterruptedException e ) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main( String[] args ) {
		new Consumer().start();
		long daltime = System.currentTimeMillis() / 1000;
		DelayItem a = new DelayItem ( "001", daltime, 1L );
		DelayItem b = new DelayItem ( "002", daltime, 3L );
		DelayItem c = new DelayItem ( "003", daltime, 5L );
		DelayItem d = new DelayItem ( "004", daltime, 2L );
		DelayItem f = new DelayItem ( "005", daltime, 10L );
		push( a );
		push( b );
		push( c );
		push( d );
		push( f );
		//移除003的告警
		DelayItem g = new DelayItem ( "003" );
		if ( queue.contains( g ) ) {
			queue.remove( g );
		}
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值