DelayQueue应用举例

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");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值