class Storage
{
public int i = 0;
}class Counter implements Runnable
{
private Storage storage;

public Counter(Storage s)
{
storage = s;
}

public void run()
{
try
{
Thread.sleep(100);
}
catch(Exception e)
{
System.out.println(e);
}
for(int i = 0; i != 100; ++i)
{
synchronized(storage)
{
storage.i = i;
System.out.println("counter: " +storage.i);
storage.notify();
}
}
}
}class Printer implements Runnable
{
private Storage storage;
public Printer(Storage s)
{
storage = s;
}

public void run()
{
for(int i = 0; i != 100; ++i)
{
synchronized(storage)
{
try
{
storage.wait();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("printer: " +storage.i);
}
}
}
}public class TestThread
{
public static void main(String[] argv)
{
Storage s = new Storage();
Thread counter = new Thread(new Counter(s));
Thread printer = new Thread(new Printer(s));
printer.start();
counter.start();
}
}
打印结果是:
counter: 0
.
.
.
counter: 97
counter: 98
counter: 99
printer: 99
然后就卡在这里了。
代码有问题吗?没发现,求高手们指点下

解决方案 »

  1.   

     private Storage storage
    这里的两个并不是同一个strorage  应该使用同一个 可以在外面创建一个 然后传递进去 这样应该就可以了
    去我的博客看看进程同步的有这个题
      

  2.   

    怎么不是呢?我在main()中是传的同一个s啊,这儿的我用来在构造函数那儿赋值的啊
      

  3.   

     Printer 里的storage 调用了Object的wait();调用之后没有被唤醒,需要加上storage.notify();才可以继续执行。 
    void wait() 
              Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
    //api 文档解释,没有被唤醒,就一直睡在那,肯定不执行了。到99的时候其实第一个进程就执行完了。
      

  4.   


    class Storage 
    {
        public int i = -1;
        boolean isWaiting=false;//是否已在等待
    }class Counter implements Runnable
    {
        private Storage storage;
        
        public Counter(Storage s)
        {
            storage = s;
        }
        
        public void run()
        {
            for(int i = 0; i != 100; ++i)
            {
              
             synchronized(storage)
                {
                    //没有消费者等待,则不创建数据
             if(!storage.isWaiting){
                     i--;
                     continue;
                    }
             System.out.println("counter: " +storage.i++);
                    storage.notify();//通知消费者去取该数据
                    try {
    storage.wait();//等待消费者去使用数据
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
                }
            }
        }
    }class Printer implements Runnable
    {
        private Storage storage;
        public Printer(Storage s)
        {
            storage = s;
        }
        
        public void run()
        {
            for(int i = 0; i != 100; ++i)
            {
               
             synchronized(storage)
                {
                    try
                    {
                     storage.isWaiting=true;//必须要先等待
                     storage.wait();//等待生产者去创建数据
                    }
                    catch(Exception e)
                    {
                        System.out.println(e);
                    }
                    System.out.println("printer: " +storage.i);
                    storage.notify();//通知生产者去创建数据
                }
            }
        }
    }public class TestThread
    {
        public static void main(String[] argv)
        {
            Storage s = new Storage();
            Thread counter = new Thread(new Counter(s));
            Thread printer = new Thread(new Printer(s));
            printer.start();
            counter.start();
        }
    }
      

  5.   

    class Storage 
    {
        public int i = 0;
        boolean isWaiting=false;//是否已在等待
    }class Counter implements Runnable
    {
        private Storage storage;
        
        public Counter(Storage s)
        {
            storage = s;
        }
        
        public void run()
        {
            for(int i = 0; i != 100; ++i)
            {
              
             synchronized(storage)
                {
                    //没有消费者等待,则不创建数据
             if(!storage.isWaiting){
                     i--;
                     continue;
                    }
             storage.i=i;
             System.out.println("counter: " +storage.i);
                    storage.notify();//通知消费者去取该数据
                    try {
    storage.wait();//等待消费者去使用数据
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
                }
            }
        }
    }class Printer implements Runnable
    {
        private Storage storage;
        public Printer(Storage s)
        {
            storage = s;
        }
        
        public void run()
        {
            for(int i = 0; i != 100; ++i)
            {
               
             synchronized(storage)
                {
                    try
                    {
                     storage.isWaiting=true;//必须要先等待
                     storage.wait();//等待生产者去创建数据
                    }
                    catch(Exception e)
                    {
                        System.out.println(e);
                    }
                    System.out.println("printer: " +storage.i);
                    storage.notify();//通知生产者去创建数据
                }
            }
        }
    }public class TestThread
    {
        public static void main(String[] argv)
        {
            Storage s = new Storage();
            Thread counter = new Thread(new Counter(s));
            Thread printer = new Thread(new Printer(s));
            printer.start();
            counter.start();
        }
    }