慢慢进入JAVA的内心世界,
今天也一直在和JAVA的语法作斗争,
到周三,写的一个基于SPRING BOOT的日志小模块,
成型啦~~
关于DelayQueue,书上是这样说的:
Student.java
package demo.thread;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class Student implements Delayed{
private String name;
private long workTime;
private long submitTime;
public String getName() {
return this.name + "交卷,用时 " + workTime;
}
public Student(String name, long submitTime) {
this.name = name;
this.workTime = submitTime;
this.submitTime = TimeUnit.NANOSECONDS.convert(submitTime, TimeUnit.MILLISECONDS )
+ System.nanoTime();
System.out.println(this.name + " 交卷, 用时 " + workTime);
}
@Override
public int compareTo(Delayed o) {
// TODO Auto-generated method stub
Student that = (Student) o;
return submitTime > that.submitTime? 1 :(submitTime < that.submitTime ? -1 :0);
}
@Override
public long getDelay(TimeUnit unit) {
// TODO Auto-generated method stub
return unit.convert(submitTime - System.nanoTime(), unit.NANOSECONDS);
}
}
ThreadMain.java
package demo.thread;
import java.util.concurrent.DelayQueue;
public class ThreadMain {
public static void main(String[] args) throws Exception {
final DelayQueue<Student> bq = new DelayQueue<Student>();
for (int i = 0; i < 5; i++) {
Student student = new Student("学生" + i, Math.round((Math.random()*10 + i)));
bq.put(student);
}
System.out.println("bq.peek();" + bq.peek().getName());
}
}
输出样子: