本帖最后由 刘文飞 于 2012-11-17 17:03 编辑
- class Resourse{
- private int count;
- public void produce()
- {
- synchronized(this)
- {
- if(count>0)
- {
- try
- {
- this.wait();
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- System.out.println("生产一个---------" + ++count );
- this.notify();
- }
- }
- public void consume()
- {
- synchronized(this)
- {
- if(count==0)
- {
- try
- {
- this.wait();
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- System.out.println("消费一个---------" + --count );
- this.notify();
- }
- }
- }
- class Producer implements Runnable
- {
- private Resource res;
- public Producer(Resource res)
- {
- this.res = res;
- }
- public void run()
- {
- while(true)
- {
- res.produce();
- }
- }
- }
- class Consumer implements Runnable
- {
- private Resource res;
- public Consumer(Resource res)
- {
- this.res = res;
- }
- public void run()
- {
- while(true)
- {
- res.consume();
- }
- }
- }
- public class ProducerConsumerDemo
- {
- public static void main(String[] args)
- {
- Resource res = new Resource();
- // new Thread(new Producer(res)).start();
- // new Thread(new Consumer(res)).start();
- Producer pro = new Producer(res);
- Consumer con = new Consumer(res);
- Thread t1 = new Thread(pro);
- Thread t2 = new Thread(con);
- t1.start();
- t2.start();
- }
- }
复制代码 G:\hezi\Code>javac ProducerConsumerDemo.java
ProducerConsumerDemo.java:53: 错误: 找不到符号
res.produce();
^
符号: 方法 produce()
位置: 类型为Resource的变量 res
ProducerConsumerDemo.java:68: 错误: 找不到符号
res.consume();
^
符号: 方法 consume()
位置: 类型为Resource的变量 res
2 个错误
————————————————————————
有什么技巧调试这些bug呢?貌似经常遇到些问题,不能一次通过啊?
————————————
谢谢了,是拼写错误,得向上去检测对象所在的类。
|