【转载】DelayQueue 的使用
  sAAkk3Vxfaa8 2023年11月13日 41 0


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().)。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
sAAkk3Vxfaa8