java多线程组件二:DelayQueue的使用方法

本篇文章主要用来介绍DeleyQueue的使用方式,根据这个类的名字就可以判断,这是一个Queue,且从它里面去的元素的顺序是按照时间的延迟来的.网上有很多例子,可是都不是很详细,我把书上的例子看了一下,然后用自己的思路写出来,原来发现里面还是有很多的误区,下面的程序是例子,加了一些注释:


/**
 * 
 */
package com.eric.concurrency;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * this class used to explain how to use delayqueue
 * @author Eric
 * 
 */
public class DelayQueueDemo {
	public static void main(String[] args) {
		try {
			ExecutorService es = Executors.newCachedThreadPool();
			DelayQueue<DelayTask> queue = new DelayQueue<DelayTask>();
			Random rand = new Random();
			// add 20's DelayTask to DelayQueue
			for (int i = 0; i < 20; i++) {
				queue.add(new DelayTask(rand.nextInt(500)));
			}
			EndTask endTaks = new EndTask(501);
			// add endtask to DelayQueue, it's time is 50001 to ensure will be
			// execute at the end of queue
			queue.add(endTaks);
			es.execute(new DelayConsumer(queue));
			TimeUnit.SECONDS.sleep(1);
			es.shutdownNow();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class DelayConsumer implements Runnable {
	private DelayQueue<DelayTask>	queue;
	
	public DelayConsumer(DelayQueue<DelayTask> queue) {
		this.queue = queue;
	}
	
	public void run() {
		try {
			System.out.println("############print order by DelayQueue###############");
			// from DelayQueue get DelayTask and execute it
			while (!Thread.interrupted()) {
				queue.take().run();
			}
		} catch (InterruptedException e) {
			System.out.println("Exit Consumer");
		}
	}
}

class EndTask extends DelayTask {
	
	public EndTask(long delay) {
		super(delay);
	}
	
	@Override
	public void run() {
		try {
			TimeUnit.MILLISECONDS.sleep(1);
			// print all task from internal list
			System.out.println("#############print by list order###############");
			for (DelayTask delayTask : tasks) {
				System.out.println("end" + delayTask);
			}
		} catch (InterruptedException e) {
			System.out.println("Exit LIST");
		}
	}
}

class DelayTask implements Runnable, Delayed {
	protected static List<DelayTask>	tasks	= new ArrayList<DelayTask>();
	public long	                     time	  = 0;
	public long	                     delay;
	private static int	             count	  = 0;
	private int	                     id	      = count++;
	
	public DelayTask(long delay) {
		this.delay = delay;
		time = System.nanoTime() + TimeUnit.NANOSECONDS.convert(this.delay, TimeUnit.MICROSECONDS);
		tasks.add(this);
	}
	
	@Override
	public int compareTo(Delayed o) {
		long result = ((DelayTask) 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 - System.nanoTime(), TimeUnit.NANOSECONDS);
	}
	
	@Override
	public void run() {
		System.out.println(this);
	}
	
	public String toString() {
		// use time to print for get real "delay time"
		return "Task " + id + ":" + time;
	}
	
	public long getTime() {
		return time;
	}
	
	public void setTime(long time) {
		this.time = time;
	}
	
}
这个程序运行的结果分两部分,一部分是从QUEUE中取出来的元素,另一部分是从普通的链表里拿出来的展示的结果.所有的元素按照相同的顺序分别放到queue和list中,最后展现的顺序是不同的,在list中的元素是按照放入的顺序取出的,而DelayQueue中的元素是按照Delay的delay因素来决定取出的顺序


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值