【转载】DelayQueue 的使用

本文介绍如何使用Java的DelayQueue来模拟一场考试的过程。通过创建实现了Delayed接口的学生对象,并利用其getDelay方法设置交卷时间,文章展示了如何有序地处理这些对象以确保按设定的时间顺序进行操作。

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

原文:http://ideasforjava.iteye.com/blog/657384

DelayQueue

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

 

Delayed

 

一种混合风格的接口,用来标记那些应该在给定延迟时间之后执行的对象。

此接口的实现必须定义一个 compareTo 方法,该方法提供与此接口的 getDelay 方法一致的排序。

 

下面的代码模拟一个考试的日子,考试时间为120分钟,30分钟后才可交卷,当时间到了,或学生都交完卷了者考试结束。线程的关闭参考Java编程思想中例子,将exec传给Student的一个内部类,通过他来关闭。

/**
 * 模拟考试,时间为120分钟,学生可以再30分钟后交卷,
 * 当学生都交完了 或 时间到者考试结束
 */
class Student implements Runnable,Delayed{
	private String name;
	private long submitTime;//交卷时间
	private long workTime;//考试时间
	public Student(String name, long submitTime) {
		this.name = name;
		workTime = submitTime;
		//都转为转为ns
		this.submitTime = TimeUnit.NANOSECONDS.convert(submitTime, TimeUnit.MILLISECONDS) + System.nanoTime();
	}
	public void run() {
		System.out.println(name + " 交卷,用时" + workTime/100 + "分钟");
	}
	public long getDelay(TimeUnit unit) {
		return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS);
	}
	public int compareTo(Delayed o) {
		Student that = (Student) o;
		return submitTime > that.submitTime?1:(submitTime < that.submitTime ? -1 : 0);
	}
	public static class EndExam extends Student{
		private ExecutorService exec;
		public EndExam(int submitTime,ExecutorService exec) {
			super(null,submitTime);
			this.exec = exec;
		}
		public void run() {
			exec.shutdownNow();
		}
	}
}
class Teacher implements Runnable{
	private DelayQueue<Student> students;
	
	public Teacher(DelayQueue<Student> students) {
		this.students = students;
	}
	public void run() {
		try {
			System.out.println("考试开始……");
			while (!Thread.interrupted()) {
				students.take().run();
			}
			System.out.println("考试结束……");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
public class Exam {
	static final int STUDENT_SIZE = 45;
	public static void main(String[] args) {
		Random r = new Random();
		DelayQueue<Student> students = new DelayQueue<Student>();
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i = 0; i < STUDENT_SIZE; i++){
			students.put(new Student("学生" + i, 3000 + r.nextInt(9000)));
		}
		students.put(new Student.EndExam(12000,exec));//1200为考试结束时间
		exec.execute(new Teacher(students));
	}
}

 

结果 写道
考试开始……
学生39 交卷,用时30分钟
学生22 交卷,用时31分钟
学生28 交卷,用时32分钟
学生40 交卷,用时34分钟
学生44 交卷,用时40分钟
学生9 交卷,用时40分钟
学生42 交卷,用时41分钟
学生25 交卷,用时44分钟
学生8 交卷,用时46分钟
学生21 交卷,用时47分钟
学生5 交卷,用时54分钟
学生10 交卷,用时55分钟
学生31 交卷,用时55分钟
学生7 交卷,用时58分钟
学生29 交卷,用时67分钟
学生37 交卷,用时69分钟
学生6 交卷,用时70分钟
学生32 交卷,用时70分钟
学生24 交卷,用时71分钟
学生30 交卷,用时74分钟
学生16 交卷,用时75分钟
学生2 交卷,用时75分钟
学生35 交卷,用时76分钟
学生34 交卷,用时80分钟
学生19 交卷,用时80分钟
学生45 交卷,用时83分钟
学生33 交卷,用时85分钟
学生13 交卷,用时86分钟
学生15 交卷,用时86分钟
学生11 交卷,用时86分钟
学生41 交卷,用时90分钟
学生38 交卷,用时91分钟
学生43 交卷,用时92分钟
学生20 交卷,用时94分钟
学生27 交卷,用时98分钟
学生36 交卷,用时99分钟
学生14 交卷,用时101分钟
学生12 交卷,用时102分钟
学生23 交卷,用时103分钟
学生17 交卷,用时105分钟
学生3 交卷,用时107分钟
学生26 交卷,用时108分钟
学生18 交卷,用时110分钟
学生1 交卷,用时114分钟
学生4 交卷,用时119分钟
考试结束……

 DelayQueue中存放了一些实现了Delayed的有序对象,其中的对象按照事情先后取走(students.take().)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值