JUC并发集合DelayQueue

 

DelayQueue 

1. DelayQueue is an unbounded queue. It extends Delayed interface.

2. Element from DelayQueue can only be taken when its delay has expired.

3. At the head of the queue , element with furthest expired delay time is found.

4. An element is expired when its getDelay() method returns a value less than or equal to zero.

5. Null is not permitted in DelayQueue. 

6. In DelayQueue, only those object can be inserted which implements Delayed interface. 

DelayQueue

是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。这种队列是有序的,即队头对象的延迟到期时间最长。注意:不能将null元素放置到这种队列中。


示例:

public class DelayQueueTest {
	
	public static void main(String[] args) {
		DelayQueue<DelayElement> queue = new DelayQueue<DelayElement>();
		long now = System.currentTimeMillis();//毫秒
		DelayElement ele1 = new DelayElement(5000+now,"e1");
		queue.offer(ele1);
		DelayElement ele2 = new DelayElement(3000+now,"e2");
		queue.offer(ele2);
		DelayElement ele3 = new DelayElement(2000+now,"e3");
		queue.offer(ele3);
		DelayElement ele4 = new DelayElement(4000+now,"e4");
		queue.offer(ele4);
		
		Iterator<DelayElement> iter = queue.iterator();
		//orignal data
		while(iter.hasNext()){
			DelayElement ele = iter.next();
			System.out.println(ele.name+":"+ele.delayTime);
		}
		//
		/*while(queue.size()>0){
			try {
				DelayElement ele = queue.take();
				System.out.println("current time in ms: " + System.currentTimeMillis() + ", element:" + ele.name); 
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}*/
		/*
		 * 多线程从队列中取数据,DelayQueue根据DelayElement的compareTo()进行排序
		 */
		ExecutorService service = Executors.newFixedThreadPool(3);
		service.execute(new DelayElementCustomer("customer01", queue));
		service.execute(new DelayElementCustomer("customer02", queue));
		service.execute(new DelayElementCustomer("customer03", queue));
		
		service.shutdown();
		System.out.println("pool shutdown now.");
		
	}
}
class DelayElementCustomer implements Runnable{//Runnable
	private DelayQueue<DelayElement> delayQueue = null;
	private String customerName;
	public DelayElementCustomer(String customerName,DelayQueue<DelayElement> delayQueue){
		this.delayQueue = delayQueue;
		this.customerName = customerName;
	}
	
	@Override
	public void run() {
		while(delayQueue.size()>0){
			DelayElement ele = null;
			try {
				ele = delayQueue.poll(1000,TimeUnit.MILLISECONDS);
				if(ele==null){
					System.out.println("delayElement in DelyQueue is not expired now."+this.customerName);
				}else{
					System.out.println("Thread "+this.customerName+" take delayElement "+(ele==null?"null":ele.name));
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			/*try {
				DelayElement ele = delayQueue.take();//queue为空时,线程阻塞。故executorservice.shutDown();一直没有关闭
				System.out.println("Thread "+this.customerName+" take delayElement "+ele.name);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}*/
		}
		System.out.println("Thread end."+this.customerName);
	}
}
class DelayElement implements Delayed{
	public long delayTime = 0L;
	public String name ;
	
	public DelayElement(long delayTime,String name){
		this.delayTime = delayTime;
		this.name = name;
	}
	
	@Override
	public long getDelay(TimeUnit unit) {//unit: TimeUnit.NANOSECONDS
		long delay = unit.convert(this.delayTime-System.currentTimeMillis(), TimeUnit.MILLISECONDS);//MILLISECONDS--> NONOSECONDS
		System.out.println(this.name+"delay long:"+delay);
		return delay;
	}

	@Override
	public int compareTo(Delayed obj) {
		DelayElement other = (DelayElement)obj;
		if(this.delayTime < other.delayTime){
			return -1;
		}else if(this.delayTime>other.delayTime){
			return 1;
		}
		return 0;
	}
	
}

 相关链接:

使用Java的DelayQueue容易碰到的一个坑

xmemcached 1.2.6.2紧急发布(升级到1.2.6.1的朋友注意)

精巧好用的DelayQueue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值