class Producer extends Thread{
private int number;
private Share shared;public Producer(Share s,int number){
shared=s;
this.number=number;
}public void run(){
for(int i=0;i<10;i++){
shared.put(i);
System.out.println("生产者"+this.number+"输出的数据为:"+i);
try{
sleep((int)(Math.random()*100));
}catch(InterruptedException e){}
}
}  
public static void main(String args[]){
Share s=new Share();
Producer p=new Producer(s,1);
Consumer c=new Consumer(s,1);
p.start();
c.start();
}
}class Consumer extends Thread{
private int number;
private Share shared;public Consumer(Share s,int number){
shared=s;
this.number=number;
}public void run(){
int value=0;
for(int i=0;i<10;i++){
value=shared.get();
System.out.println("消费者"+this.number+"得到的数据:"+value);
}
}}class Share{
private int contents;
private boolean available=false;public synchronized int get(){
while(available==false){
try{
wait();
}catch (InterruptedException e){}
available=false;
notifyAll();
return contents;
}public synchronized void put(int value){
while(available==true){
try{
wait();
}catch(InterruptedException e){}
contents=value;
available=ture;
notifyAll();
}
}这程序有何问题 3ERRORS
public synchronized void put(int value)没有这种用法?
shared.put(i)??

解决方案 »

  1.   

    有几个很简单的错误。请参考我修改后的
    package test.thread;public class Producer extends Thread {
      private int number;  private Share shared;  public Producer(Share s, int number) {
        shared = s;
        this.number = number;
      }  public void run() {
        for (int i = 0; i < 10; i++) {
          shared.put(i);
          System.out.println("生产者" + this.number + "输出的数据为:" + i);
          try {
            sleep((int) (Math.random() * 100));
          } catch (InterruptedException e) {}
        }
      }  public static void main(String args[]) {
        Share s = new Share();
        Producer p = new Producer(s, 1);
        Consumer c = new Consumer(s, 1);
        p.start();
        c.start();
      }
    }class Consumer extends Thread {
      private int number;  private Share shared;  public Consumer(Share s, int number) {
        shared = s;
        this.number = number;
      }  public void run() {
        int value = 0;
        for (int i = 0; i < 10; i++) {
          value = shared.get();
          System.out.println("消费者" + this.number + "得到的数据:" + value);
        }
      }
    }class Share {
      private int contents;  private boolean available = false;  public synchronized int get() {
        while (available == false) {
          try {
            wait();
          } catch (InterruptedException e) {}
        } // 扩号位置错误,应该挪到这里
        available = false;
        notifyAll();
        return contents;
      }  public synchronized void put(int value) {
        while (available == true) {
          try {
            wait();
          } catch (InterruptedException e) {}
        } // 扩号位置错误,应该挪到这里
        contents = value;
        available = true; // 拼写错误,没有ture
        notifyAll();
      }
    }运行结果
    生产者1输出的数据为:0
    消费者1得到的数据:0
    生产者1输出的数据为:1
    消费者1得到的数据:1
    生产者1输出的数据为:2
    消费者1得到的数据:2
    消费者1得到的数据:3
    生产者1输出的数据为:3
    生产者1输出的数据为:4
    消费者1得到的数据:4
    生产者1输出的数据为:5
    消费者1得到的数据:5
    生产者1输出的数据为:6
    消费者1得到的数据:6
    生产者1输出的数据为:7
    消费者1得到的数据:7
    生产者1输出的数据为:8
    消费者1得到的数据:8
    生产者1输出的数据为:9
    消费者1得到的数据:9
      

  2.   

    package test;class Producer extends Thread {
    private int number; private Share shared; public Producer(Share s, int number) {
    shared = s;
    this.number = number;
    } public void run() {
    for (int i = 0; i < 10; i++) {
    shared.put(i);
    System.out.println("生产者" + this.number + "输出的数据为:" + i);
    try {
    sleep((int) (Math.random() * 100));
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    } public static void main(String args[]) {
    Share s = new Share();
    Producer p = new Producer(s, 1);
    Consumer c = new Consumer(s, 1);
    p.start();
    c.start();
    // p.notifyAll();
    // c.notifyAll();
    }
    }class Consumer extends Thread {
    private int number; private Share shared; public Consumer(Share s, int number) {
    shared = s;
    this.number = number;
    } public void run() {
    int value = 0;
    for (int i = 0; i < 10; i++) {
    value = shared.get();
    System.out.println("消费者" + this.number + "得到的数据:" + value);
    }
    }}class Share {
    private int contents; private boolean available = false; public synchronized int get() {
    while (available == false) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    available = false;
    notifyAll();

    }
    return contents;---------返回值要寫在最后。。
    } public synchronized void put(int value) {
    while (available == true) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    contents = value;
    boolean ture = false;----賦值的true沒定義
    available = ture;
    notifyAll();
    }
    }
    }
    好了
    你手動編譯的?
    建議不要你那樣寫。。因為后面程序會繼續運行下去。。
      

  3.   

    呵呵!你那个括号我也是仔细配对之后才发现的。
    逻辑上应该在那个位置。至于错误,大括号引起的错误可不是好找的。写在里面,写在外面可能就完全不同。建议你用IDE吧。 Eclipse 挺好的,可以防范这类问题。
      

  4.   

    谢谢 我刚学的 是用记事本的  下次用ECLIPSE
      

  5.   

    http://www.java2000.net/p388
    请参考Eclipse的安装和内存配置
      

  6.   

    1.如果使用了自动提示的IDE 一些拼写和格式错误应该可以在编译器被避免
    2.如果为了实现两个线程轮替执行的效果 利用同步两个方法和传入的同一个对象的布尔值进行保证
    同步方法确保了两个线程在各自访问方法时不会相继wait,有效地避免了死锁。