有这样的代码:class myThread extends Thread
    {
    Myobj obj;//这是一个线程安全的对象,比如Vector
    public void run()
        {
        while(true)
            {
            if(obj.state=="ready")
                {
                process(obj);
                obj.state="notready";
                }
            }
        }
    public void setready()
        {
        this.obj.state="ready";
        }  
    }setready()方法由构造这个线程的代码调用.
现在我想让这个线程阻塞直到obj.state=="ready",处理后再次进入阻塞.应该怎么实现?
谢谢了~

解决方案 »

  1.   

    while(true){
      if(!"ready".equals(obj.state)){
        Thread.sleep(1000); // 我看你休眠比较好。
      }else{
        .... // 正常的操作
      }
    }
      

  2.   

    就是说,不用while,想要实现成不占CPU的等待.
      

  3.   

    阻塞需要同步一个对象,我想并不适合你的操作。比如while(true){
      synchronized(obj){
         if(!"ready".equals(obj.state)){
           wait();  这个要求对方同样用obj作为同步对象,并发出nodify.notifAll方法。
         }else{
            。//
         }
      }
    }
      

  4.   

    一般可以用
    public static final Myobj obj = ...; 作为同步的对象,方便程序操作。
      

  5.   

    我那个代码已经阻塞在 obj 上了,你的另外的一个线程,负责对obj进行设置,并调用 nodify方法通知这面。
      

  6.   

    是的,不太理解JAVA的这个机制,想用但不会用...
      

  7.   

    这是符合你所要求的一个例子,Tabale-->餐桌(测试程序中为大小为10的队列),Chef-->厨师(生产者,不断往餐桌上放Food,也就是往队列里加东东,如果餐桌满了,则等待食客通知再添加Food),Eater--食客(消费者,不断从餐桌上取Food,也就是从队列里取东东,每次取东东后则通知厨师可以往餐桌上再放东东了)package debug;
    import java.util.regex.*;
    import java.util.*;
    class Food{}class Table extends LinkedList{
      int maxSize;
      public Table(int maxSize){
        this.maxSize = maxSize;
      }
      public synchronized void putFood(Food f){
        while(this.size() >= this.maxSize){
          try{
            this.wait();
          }catch(Exception e){}
        }
        this.add(f);
        notifyAll();
      }
      
      public synchronized Food getFood(){
        while(this.size() <= 0){
          try{
            this.wait();
          }catch(Exception e){}
        }
        Food f = (Food)this.removeFirst();
        notifyAll();
        return f;
      }
    }
    class Chef extends Thread{
      Table t;
      String name;
      Random r = new Random(12345);
      public Chef(String name,Table t){
        this.t = t;
        this.name = name;
      }
      public void run(){
        while(true){
          Food f = make();
          System.out.println(name+" put a Food:"+f);
          t.putFood(f);
        }
      }
      private Food make(){
        try{
          Thread.sleep(200+r.nextInt(200));
        }catch(Exception e){}
        return new Food();
      }
    }class Eater extends Thread{
      Table t;
      String name;
      Random r = new Random(54321);
      public Eater(String name,Table t){
        this.t = t;
        this.name = name;
      }
      public void run(){
        while(true){
          Food f = t.getFood();
          System.out.println(name+" get a Food:"+f);
          eat(f);
          
        }
      }
      private void eat(Food f){
        
        try{
          Thread.sleep(400+r.nextInt(200));
        }catch(Exception e){}
      }
    }public class Test {
        public static void main(String[] args) throws Exception{
          Table t = new Table(10);
          new Chef("Chef1",t).start();
          new Chef("Chef2",t).start();
          new Chef("Chef3",t).start();
          new Chef("Chef4",t).start();
          new Eater("Eater1",t).start();
          new Eater("Eater2",t).start();
          new Eater("Eater3",t).start();
          new Eater("Eater4",t).start();
          new Eater("Eater5",t).start();
          new Eater("Eater6",t).start();    }
    }