可以参考http://blog.csdn.net/skywalker_only/article/details/38871303,在主线程中调用t.join(),假设t为插队的线程

解决方案 »

  1.   

    把需要先执行的线程设置优先级,就是调用join()方法
      

  2.   


    排两个队呗。VIP一个队,普通人一个队。
      

  3.   

    楼主的需求不复杂啊。  楼上几位想多了吧。
    楼主的需求是等公司当前那个人吃完饭后,就是VIP们只插队,但是不会把吃饭的人赶下桌子。
    那你建2个排队的队列,普通队列和VIP队列,比如现在有3个桌子供吃饭,每当有桌子空出来,就先去VIP队列中看看有没有人在等,没有的话再去普通人队列中寻找,就OK了啊。
      

  4.   

    队列啊,把要插队的人直接插到队列前面。Deque<Object> deque = new LinkedList<Object>(); // 插队
    deque.offerFirst(new Object()); // 排队
    deque.offerLast(new Object());
      

  5.   

    list也可以搞定
    //插队,想插到哪里都行
    list.add(0,xxx);
    //排队
    list.add(xxx);
      

  6.   

    用 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);
    }
    }
      

  7.   

    优先级肯定不行。去看java优先级的定义。
      

  8.   

    可以用join,也可以用类似于linux pid文件方式,有人来就生成一个锁文件。
    www.4byte.cn
      

  9.   


    怎么区分VIP队列和普通队列啊?
    程序又怎么判断的。
      

  10.   

    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();
    }
      

  11.   

    用nofity()方法实现线程通信,再设置线程优先级priority