关于线程等待问题 可以参考http://blog.csdn.net/skywalker_only/article/details/38871303,在主线程中调用t.join(),假设t为插队的线程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把需要先执行的线程设置优先级,就是调用join()方法 排两个队呗。VIP一个队,普通人一个队。 楼主的需求不复杂啊。 楼上几位想多了吧。楼主的需求是等公司当前那个人吃完饭后,就是VIP们只插队,但是不会把吃饭的人赶下桌子。那你建2个排队的队列,普通队列和VIP队列,比如现在有3个桌子供吃饭,每当有桌子空出来,就先去VIP队列中看看有没有人在等,没有的话再去普通人队列中寻找,就OK了啊。 队列啊,把要插队的人直接插到队列前面。Deque<Object> deque = new LinkedList<Object>(); // 插队 deque.offerFirst(new Object()); // 排队 deque.offerLast(new Object()); list也可以搞定//插队,想插到哪里都行list.add(0,xxx);//排队list.add(xxx); 用 JOIN方法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); }} 优先级肯定不行。去看java优先级的定义。 可以用join,也可以用类似于linux pid文件方式,有人来就生成一个锁文件。www.4byte.cn 怎么区分VIP队列和普通队列啊?程序又怎么判断的。 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(); } 用nofity()方法实现线程通信,再设置线程优先级priority 请高手指点,关于数据库的问题 帮我修改个GUI题目 谢谢 一道小题 有段代码,问两个问题 问一个基础问题:关于运算符的。(没有分了,我现在去回帖再揭贴放分.) 菜鸟问题:如何得到开发帮助? 请问如何用JAVA获取主机的所有驱动器路径 请问在Win98中jdk1.4.0怎样设置路径? 关于JDBC,如何执行嵌套数据库的查询 main(String args[])与main(String[] args)有什么区别? 关于使用递归求排列组合,并如何学习递归! JAVA同步块,让我纠结了好久
排两个队呗。VIP一个队,普通人一个队。
楼主的需求是等公司当前那个人吃完饭后,就是VIP们只插队,但是不会把吃饭的人赶下桌子。
那你建2个排队的队列,普通队列和VIP队列,比如现在有3个桌子供吃饭,每当有桌子空出来,就先去VIP队列中看看有没有人在等,没有的话再去普通人队列中寻找,就OK了啊。
deque.offerFirst(new Object()); // 排队
deque.offerLast(new Object());
//插队,想插到哪里都行
list.add(0,xxx);
//排队
list.add(xxx);
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);
}
}
www.4byte.cn
怎么区分VIP队列和普通队列啊?
程序又怎么判断的。
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();
}