import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class User implements Delayed {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String userName;
private String passWord;
private Boolean enable;
private Date loginTime; //登录时间
private Date timeOut; //超时时间
private String status = "LOGINNING"; //状态
public String getStatus() {
return status;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", enable=" + enable +
", loginTime=" + sdf.format(loginTime) +
", timeOut=" + sdf.format(timeOut) +
", status='" + status + '\'' +
'}';
}
public void setStatus(String status) {
this.status = status;
}
public User(String userName, String passWord, Boolean enable, Date loginTime, Date timeOut) {
this.userName = userName;
this.passWord = passWord;
this.enable = enable;
this.loginTime = loginTime;
this.timeOut = timeOut;
}
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Override
public long getDelay(TimeUnit unit) { //用来做排序和超时判断的
long l = unit.convert(timeOut.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
System.out.println(userName + "==>getDelay:" + l);
return l;
}
public Date getLoginTime() {
return loginTime;
}
public void setLoginTime(Date loginTime) {
this.loginTime = loginTime;
}
public Date getTimeOut() {
return timeOut;
}
public void setTimeOut(Date timeOut) {
this.timeOut = timeOut;
}
@Override
public int compareTo(Delayed o) { //用来做排序的
return this.getTimeOut().compareTo(((User) o).getTimeOut());
}
}
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.DelayQueue;
public class DelayQueueTest {
//是否开启自动取消功能
int isStarted = 1;
//延迟队列,用来存放订单对象
DelayQueue<User> queue = new DelayQueue();
public static void main(String[] args) {
DelayQueueTest testDelayQueue = new DelayQueueTest();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//新建一个线程,用来模拟定时取消订单job
Thread t1 = new Thread(() -> {
System.out.println("开启自动取消订单job,当前时间:" + LocalDateTime.now().format(formatter));
while (testDelayQueue.isStarted == 1) {
System.out.println("正在寻找订单");
try {
User order = testDelayQueue.queue.take();
order.setStatus("CANCELED");
System.out.println("订单:" + order.getUserName() + "付款超时,自动取消,当前时间:" + LocalDateTime.now().format(formatter));
//取消订单
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
//新建一个线程,模拟提交订单
Thread t2 = new Thread(() -> {
//定义最早的订单的创建时间
long beginTime = System.currentTimeMillis();
//下面模拟6个订单,每个订单的创建时间依次延后3秒
testDelayQueue.queue.add(new User("SO001", "A", true, new Date(beginTime), new Date(beginTime + 50000)));
testDelayQueue.queue.add(new User("SO002", "A", true, new Date(beginTime), new Date(beginTime + 40000)));
testDelayQueue.queue.add(new User("SO003", "A", true, new Date(beginTime), new Date(beginTime + 30000)));
testDelayQueue.queue.add(new User("SO004", "A", true, new Date(beginTime), new Date(beginTime + 10000)));
testDelayQueue.queue.add(new User("SO005", "C", true, new Date(beginTime), new Date(beginTime + 20000)));
testDelayQueue.queue.add(new User("SO006", "C", true, new Date(beginTime), new Date(beginTime + 80000)));
System.out.println(testDelayQueue.queue.toString());
});
t2.start();
}
}