62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Queue;
class TestQueue {
PriorityQueue VIPqueue = new LinkedList<String>();
Queue 普通queue = new LinkedList<String>();;
Thread autoThread;
public VIP(){
println "VIP的人来抢饭吃了,警报-------------------------"
VIPqueue.add("小米");
VIPqueue.add("小明");
VIPqueue.add("小敏");
VIPqueue.add("小蜜");
VIPqueue.add("消灭");
VIPqueue.add("小名");
VIPqueue.add("校门");
吃饭(VIPqueue);
VIPqueue.remove();
println "VIP的人全部吃完饭了。。。。"
}
public 吃饭(Queue queue){
queue.each {name ->
println name + ":吃饭ing....";
this.sleep(1000);
}
}
public 普通(){
autoThread = new Thread(new Runnable(){
public void run(){
while((new Date()).format("HH") < "17"){
普通queue.add("北鼻");
普通queue.add("贝贝");
普通queue.add("北北");
普通queue.add("背包");
普通queue.add("背部");
普通queue.add("北边");
普通queue.add("北碚");
println "普通的人开始吃饭了**********************"
吃饭(普通queue);
println "普通的人全部吃完饭了。。。。睡2分钟,然后再继续吃。。。。"
this.sleep(2 * 60 * 1000);
}
}
});
autoThread.start();
}
public static void main(def arg){
TestQueue tq = new TestQueue();
tq.普通();
this.sleep(3000);
tq.VIP();
}
class Sleeper extends Thread{
private int duration;
public Sleeper(String name,int sleepTime){
super(name);
duration=sleepTime;
start();
}
public void run(){
try{
for(int i=0;i<50;i++){
System.out.println(i);
}
sleep(duration);
}catch(InterruptedException e){
System.out.println(getName() + "was interrupted" + "is Interrupted" + isInterrupted());
return;
}
System.out.println(getName()+ "has awakened");
}
}
class Joiner extends Thread{
private Sleeper sleeper;
public Joiner(String name,Sleeper sleeper){
super(name);
this.sleeper=sleeper;
start();
}
public void run(){
try{
sleeper.join();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(getName()+ "join completed");
}
}
public class Joining {
public static void main(String[] arg){
Sleeper s1= new Sleeper("sleepy",2000),
s2=new Sleeper("grumpy",2000);
Joiner j1= new Joiner("dopey",s1),
j2=new Joiner("doc",s2);
}
}
Deque<Object> deque = new LinkedList<Object>();
// 插队
deque.offerFirst(new Object());
// 排队
deque.offerLast(new Object());