【Java並發編程】3、DelayQueue應用場景,多考生考試。本站提示廣大學習愛好者:(【Java並發編程】3、DelayQueue應用場景,多考生考試)文章只能為提供參考,不一定能成為您想要的結果。以下是【Java並發編程】3、DelayQueue應用場景,多考生考試正文
該場景來自於:http://www.cnblogs.com/sunzhenchao/p/3515085.html。
模擬一個考試的日子,考試時間為120分鐘,30分鐘後才可交卷,當時間到了,或學生都交完卷了考試結束。
這個場景中幾個點需要注意:
抽象出兩個類,學生類和老師類,用DelayQueue存儲考生(Student類)。每一個考生都有自己的名字和完成試卷的時間
Teacher線程對DelayQueue進行監控,收取完成試卷小於120分鐘的學生的試卷。當考試時間120分鐘到時,teacher線程宣布考試結束,強制DelayQueue中還存在的考生交卷。
package zhongqiu.common.base.thread; import java.util.Iterator; import java.util.Random; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; public class DelayQueueDemoOne { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub int studentNumber = 20; DelayQueue<Student> students = new DelayQueue<Student>(); Random random = new Random(); for (int i = 0; i < studentNumber; i++) { students.put(new Student("student" + (i + 1), 30 + random.nextInt(120))); } students.put(new Student("student",120)); Thread teacherThread = new Thread(new Teacher(students)); teacherThread.start(); } } class Student implements Runnable, Delayed { private String name; public long workTime; private long submitTime; private boolean isForce = false; public Student() { } public Student(String name, long workTime) { this.name = name; this.workTime = workTime; this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS) + System.nanoTime();// 納秒級別 } @Override public int compareTo(Delayed o) { // TODO Auto-generated method stub if (o == null || !(o instanceof Student)) return 1; if (o == this) return 0; Student s = (Student) o; if (this.workTime > s.workTime) { return 1; } else if (this.workTime == s.workTime) { return 0; } else { return -1; } } @Override public long getDelay(TimeUnit unit) { // TODO Auto-generated method stub return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS); } @Override public void run() { // TODO Auto-generated method stub if (isForce) { System.out.println(name + " 交卷,實際用時 120分鐘"); } else { System.out.println(name + " 交卷," + "實際用時 " + workTime + " 分鐘"); } } public boolean isForce() { return isForce; } public void setForce(boolean isForce) { this.isForce = isForce; } } class Teacher implements Runnable { private int counter = 20; private DelayQueue<Student> students; public Teacher(DelayQueue<Student> students) { this.students = students; } @Override public void run() { // TODO Auto-generated method stub try { System.out.println(" test start"); while (counter > 0) { Student student = students.poll(); if (student.workTime<120) { student.run(); if (counter > 0) { counter--; } } else { System.out.println(" 考試時間到,全部交卷!"); Student tmpStudent; for (Iterator<Student> iterator2 = students.iterator(); iterator2.hasNext();) { tmpStudent = iterator2.next(); tmpStudent.setForce(true); tmpStudent.run(); if (counter > 0) { counter--; } } } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }