-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathUseDelayQueue.java
More file actions
54 lines (40 loc) · 1.51 KB
/
Copy pathUseDelayQueue.java
File metadata and controls
54 lines (40 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.msj.sync.queue;
import java.util.concurrent.DelayQueue;
/**
* @author Vincent.M mengshaojie@188.com
* @date 2017/3/14 下午7:54
* @copyright ©2017 孟少杰 All Rights Reserved
* @desc 使用DelayQueue 根据网民上网的下机来说明使用
*/
public class UseDelayQueue implements Runnable{
DelayQueue<WangMin> delayQueue = new DelayQueue<>();
public boolean yinye = true;
public void shangji(int id,String name,int money){
WangMin wangMin = new WangMin(id,name,1000*money+System.currentTimeMillis());
System.out.println("网名:" + wangMin.getName()+ "id:"+ wangMin.getId()+ "交钱"+ money +"块,开始上机");
this.delayQueue.add(wangMin);
}
public void xiaji(WangMin wangMin){
System.out.println("网名:" + wangMin.getName()+ "id:"+ wangMin.getId()+ "时间到下机");
}
@Override
public void run() {
while(yinye){
try{
WangMin wangMin = delayQueue.take();
xiaji(wangMin);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
public static void main(String[] args){
System.out.println("网吧开始营业");
UseDelayQueue useDelayQueue = new UseDelayQueue();
Thread shangwang = new Thread(useDelayQueue);
shangwang.start();
useDelayQueue.shangji(1,"路人甲",3);
useDelayQueue.shangji(2,"路人乙",8);
useDelayQueue.shangji(3,"路人丙",6);
}
}