import java.util.Iterator;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class ExamDelayQueue {
public static void main(String[] args){
testExam();
/*System.out.println((new Random()).nextInt(10));
System.out.println((new Random()).nextInt(10));
System.out.println(System.nanoTime());// 现在的时间。
*/ //System.out.println(System.currentTimeMillis());//从2017到现在的秒数。
}
public static void testExam(){
DelayQueue<Student> delay = new DelayQueue<Student>();
int studentCount = 20;
CountDownLatch countDown = new CountDownLatch(studentCount );
for(int i = 0; i < studentCount; i++){
delay.offer(new Student("student" + i, 3000 + new Random().nextInt(10000) , countDown));
}
Teacher teacher = new Teacher(delay);
Thread teacherThread = new Thread(teacher);
delay.offer(new EndExam("end exam" , 10000 , countDown , teacherThread , delay));
teacherThread.start();
try {
countDown.await();
System.out.println("结束考试!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Student implements Delayed,Runnable{
long EndTime;
String name;
long delayTime;
CountDownLatch countDown ;
boolean isForce = false;
public Student(String name, long delayTime , CountDownLatch countDown){
this.EndTime = System.currentTimeMillis() + delayTime;
this.name = name;
this.delayTime = delayTime;
this.countDown = countDown;
}
public long getDelay(TimeUnit u){// 剩余时间
return this.EndTime - System.currentTimeMillis();
}
public int compareTo(Delayed d){
return (int)(this.getDelay(TimeUnit.MILLISECONDS) - d.getDelay(TimeUnit.MILLISECONDS) );
}
public void run(){
if (isForce){
System.out.println("交卷,"+this.name+",强制交卷:"+10+"秒");
} else {
System.out.println("交卷,"+this.name+",使用时间:"+delayTime);
}
countDown.countDown();
}
public void setForce(){
isForce = true;
}
}
class EndExam extends Student implements Runnable{
private Thread teacherThread ;
private DelayQueue<Student> delayQueue;
public EndExam(String name, long delayTime , CountDownLatch countDown , Thread teacherThread , DelayQueue<Student> delayQueue){
super(name , delayTime , countDown);
this.teacherThread = teacherThread;
this.delayQueue = delayQueue;
}
public void run(){
Iterator<Student> it = this.delayQueue.iterator();
while(it.hasNext()){
Student temp = it.next();
temp.setForce();
temp.run();
}
teacherThread.interrupt();// 运行完后,interrupt,teacher线程。
}
}
class Teacher implements Runnable{
DelayQueue<Student> delayQueue = new DelayQueue<Student>();
public Teacher(DelayQueue<Student> delayQueue){
this.delayQueue = delayQueue;
}
public void run(){
while(!Thread.interrupted()){
try {
delayQueue.take().run();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("ok");
}
}
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class ExamDelayQueue {
public static void main(String[] args){
testExam();
/*System.out.println((new Random()).nextInt(10));
System.out.println((new Random()).nextInt(10));
System.out.println(System.nanoTime());// 现在的时间。
*/ //System.out.println(System.currentTimeMillis());//从2017到现在的秒数。
}
public static void testExam(){
DelayQueue<Student> delay = new DelayQueue<Student>();
int studentCount = 20;
CountDownLatch countDown = new CountDownLatch(studentCount );
for(int i = 0; i < studentCount; i++){
delay.offer(new Student("student" + i, 3000 + new Random().nextInt(10000) , countDown));
}
Teacher teacher = new Teacher(delay);
Thread teacherThread = new Thread(teacher);
delay.offer(new EndExam("end exam" , 10000 , countDown , teacherThread , delay));
teacherThread.start();
try {
countDown.await();
System.out.println("结束考试!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Student implements Delayed,Runnable{
long EndTime;
String name;
long delayTime;
CountDownLatch countDown ;
boolean isForce = false;
public Student(String name, long delayTime , CountDownLatch countDown){
this.EndTime = System.currentTimeMillis() + delayTime;
this.name = name;
this.delayTime = delayTime;
this.countDown = countDown;
}
public long getDelay(TimeUnit u){// 剩余时间
return this.EndTime - System.currentTimeMillis();
}
public int compareTo(Delayed d){
return (int)(this.getDelay(TimeUnit.MILLISECONDS) - d.getDelay(TimeUnit.MILLISECONDS) );
}
public void run(){
if (isForce){
System.out.println("交卷,"+this.name+",强制交卷:"+10+"秒");
} else {
System.out.println("交卷,"+this.name+",使用时间:"+delayTime);
}
countDown.countDown();
}
public void setForce(){
isForce = true;
}
}
class EndExam extends Student implements Runnable{
private Thread teacherThread ;
private DelayQueue<Student> delayQueue;
public EndExam(String name, long delayTime , CountDownLatch countDown , Thread teacherThread , DelayQueue<Student> delayQueue){
super(name , delayTime , countDown);
this.teacherThread = teacherThread;
this.delayQueue = delayQueue;
}
public void run(){
Iterator<Student> it = this.delayQueue.iterator();
while(it.hasNext()){
Student temp = it.next();
temp.setForce();
temp.run();
}
teacherThread.interrupt();// 运行完后,interrupt,teacher线程。
}
}
class Teacher implements Runnable{
DelayQueue<Student> delayQueue = new DelayQueue<Student>();
public Teacher(DelayQueue<Student> delayQueue){
this.delayQueue = delayQueue;
}
public void run(){
while(!Thread.interrupted()){
try {
delayQueue.take().run();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("ok");
}
}