日期:2014-05-20  浏览次数:20662 次

菜鸟求救啊,大侠们给我细细讲解下啊
这是一个模拟生产和消费的程序,为什么会出错
class Zhan{
private char[] date = new char[6]; 
private int cnt;

public synchronized void push(char ch){
while(cnt == date.length){
try{
this.wait();
}
catch(Exception e){

}
}
this.notify();
date[cnt] = ch;
++cnt;
System.out.printf("生产线程正在生产第%d个产品,这个产品是:%c\n",cnt,date[cnt]);
}
public synchronized int pop(){
while(cnt == 0){
try{
this.wait();
}
catch(Exception e){

}
}
this.notify();
--cnt;
System.out.printf("消费线程正在消费第%d个产品,这个产品是:%c\n",cnt+1,date[cnt]);
return cnt;
}
}
class Sheng implements Runnable{
Zhan z = null;
char ch;

public Sheng(Zhan z){
this.z = z;
}
public void run(){

for(int i = 0;i < 20;i++){
ch = (char)('a'+ i);
z.push(ch);
}
}
}
class Xiao implements Runnable{
Zhan z = null;
public Xiao(Zhan z){
this.z = z;

}
public void run(){
for(int i = 0;i < 20;i++){
z.pop();
}
}
}
class H{
public static void main(String[] args){
Zhan z = new Zhan();
Sheng s = new Sheng(z);
Xiao x = new Xiao(z);
Thread t1 = new Thread(s);
Thread t2 = new Thread(x);

t1.start();
t2.start();

}
}


------解决方案--------------------
class Zhan {
private char[] date = new char[6];
private int cnt;

public synchronized void push(char ch) {
while (cnt == date.length) {
try {
this.wait();
} catch (Exception e) {

}
}
this.notify();
date[cnt] = ch;
System.out.printf("生产线程正在生产第%d个产品,这个产品是:%c\n", cnt, date[cnt]);
cnt++;
}

public synchronized int pop() {
while (cnt == 0) {
try {
this.wait();
} catch (Exception e) {

}
}
this.notify();
--cnt;
System.out.printf("消费线程正在消费第%d个产品,这个产品是:%c\n", cnt + 1, date[cnt]);
return cnt;
}
}

class Sheng implements Runnable {
Zhan z = null;
char ch;

public Sheng(Zhan z) {
this.z = z;
}

public void run() {

for (int i = 0; i < 20; i++) {
ch = (char) ('a' + i);
z.push(ch);
}
}
}

class Xiao implements Runnable {
Zhan z = null;

public Xiao(Zhan z) {
this.z = z;

}

public void run() {
for (int i = 0; i < 20; i++) {
z.pop();
}
}
}

public class H {
public static void main(String[] args) {
Zhan z = new Zhan();
Sheng s = new Sheng(z);
Xiao x = new Xiao(z);
Thread t1 = new Thread(s);
Thread t2 = new Thread(x);

t1.start();
t2.start();

}
}
主要就是,就数组下标问题
------解决方案--------------------
Java code

import java.util.ArrayList;
import java.util.List;

class Zhan{
    private List<Character> date = new ArrayList<Character>();
    private int cnt;
    private int MaxCount = 6;
    public synchronized void push(char ch) {
        while (cnt == MaxCount) {
            try {
                this.wait();
            } catch (Exception e) {

            }
        }
        this.notify();
        date.add(ch);
        cnt++;
        System.out.printf("生产线程正在生产第%d个产品,这个产品是:%c\n", cnt,date.get(cnt-1));
        
    }

    public synchronized int pop(){
        while (cnt == 0) {
            try {
                this.wait();
            } catch (Exception e) {

            }
        }
        this.notify();
        cnt--;
        System.out.printf("消费线程正在消费第%d个产品,这个产品是:%c\n", cnt+1,date.get(cnt));
        date.remove(cnt);
        return cnt;
    }

    public void run() {
        
    }
}

class Sheng implements Runnable {
    Zhan z = null;
    char ch;
    public Sheng(Zhan z) {
        this.z = z;
    }
    public void run() {
        for (int i = 0; i < 20; i++){
            ch = (char)('a' + i);
            z.push(ch);
        }
    }
}

class Xiao implements Runnable {
    Zhan z = null;
    public Xiao(Zhan z) {
        this.z = z;

    }
    public void run() {
        for (int i = 0; i < 20; i++){
            z.pop();
        }
    }
}

public class H {
    public static void main(String[] args) {
        Zhan z = new Zhan();
        Sheng s = new Sheng(z);
        Xiao x = new Xiao(z);
        Thread t1 = new Thread(s);
        Thread t2 = new Thread(x);
        t1.start();
        t2.start();

    }
}