一个线程的代码,,可以运行,但结果有问题,各位给看看,谢了。
做一个Stack类
package MyPackage;public class Stack {
char[] buffer=new char[6];
int index=0;

public synchronized void push(char c){
while(index==buffer.length){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
buffer[index]=c;
index++;
notify();
}

public synchronized char pop(){
while(index==0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
index--;
char c=buffer[index];
notify();
return c;
}
}做一个Producer类
package MyPackage;public class Producer extends Thread{
private Stack st;
public Producer(Stack s){
this.st=s;
}

public void run(){
char ch;
for(int i=0;i<1000;i++){
ch=(char)(Math.random()*26+'A');
st.push(ch);
System.out.println("生产了:"+ch+" ,放置在仓库的"+(st.index-1)+"位置");
}
}
}
做一个Consumer类
package MyPackage;public class Consumer extends Thread{
private Stack st;
public Consumer(Stack s){
this.st=s;
}

public void run(){
char ch;
for(int i=0;i<1000;i++){
ch=st.pop();
System.out.println("消费了:"+ch+" ,从在仓库的"+(st.index)+"位置");
}
}
}
最后是MainClass
package MyPackage;public class MainClass { public static void main(String[] args) {
Stack s=new Stack();
Producer xwh=new Producer(s);
Consumer zzh=new Consumer(s);

xwh.start();
zzh.start();

}}
各位给看下那里错了,,

解决方案 »

  1.   

    答:
    1)
    public synchronized void push(char c){ 
    while(index==buffer.length){ //改为:index>=buffer.length-1
    try { 
    wait(); 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 


    buffer[index]=c; //改为:buffer[++index]=c;
    index++; 
                   //这个不要了。
    notify(); //改为:notifyAll();更好些

    )2)
    public synchronized char pop(){ 
    while(index==0){ //改为:index<0
    try { 
    wait(); 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 


    index--; //不要了
    char c=buffer[index]; //改为:char c=buffer[index--];
    notify(); //改为:notifyAll(); 
    return c; 


    以上仅供你参考
      

  2.   

    答:另外:int index=0;  //初始值改为:int index=-1;
      

  3.   

    LZ是想用线程实现生产消费者模式吧?
      你的代码判断条件有问题!
        偶前几天也写了个,贴出来供大家参考:
           public class Producyter_buyer {
    private int size = 4;
    private int remain = size; public static void main(String[] args) {
    Producyter_buyer pb = new Producyter_buyer();
    pb.start();
    } public void start() {
    producter p = new producter();
    buyer b = new buyer();
    p.start();
    b.start();
    }
    class producter extends Thread {
    public void run() {
    for (int i = 0; true; i++) {
    product(i);
    try {
    this.sleep((int) Math.random() * 5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    class buyer extends Thread {
    public void run() {
    for (int i = 0; true; i++) {
    buy(i);
    try {
    this.sleep((int) Math.random() * 5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    } public synchronized void product(int i) {
    while (remain < this.size) {
    remain++;
    System.out.println("被生产,库存为----------------------->" + remain);
    if (remain >= this.size) {
    System.out.println("生产停止,库存满为" + remain);
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notify();
    }
    } public synchronized void buy(int i) {
    while (remain <= this.size) {
    remain--;
    System.out.println("被消费,库存为----------------------->" + remain);
    if (remain <= 1) {
    System.out.println("消费停止,库存仅为" + remain);
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    notifyAll();
    }
    }
    }
      

  4.   

    答:这个e.printStackTrace(); 可以注释掉
      

  5.   

    涉及到2个线程时,用notify也一样!
      

  6.   

    答:不会吧?
    代码如下:你试一下就知道了package MyPackage;
    public class Stack { 
    char[] buffer=new char[6]; 
    int index=-1;  public synchronized void push(char c){ 
    while(index>=buffer.length-1){ 
    try { 
    wait(); 
    } catch (InterruptedException e) { 
    //e.printStackTrace(); 


    buffer[++index]=c; 
    System.out.println("生产了:"+c+" ,放置在仓库的"+(index)+"位置"); 
    notifyAll(); 
    }  public synchronized char pop(){ 
    while(index<0){ 
    try { 
    wait(); 
    } catch (InterruptedException e) { 
    //e.printStackTrace(); 



    char c=buffer[index--]; 
    System.out.println("消费了:"+c+" ,从在仓库的"+(index+1)+"位置"); 
    notifyAll(); 
    return c; 


    package MyPackage;
    public class Producer extends Thread{ 
    private Stack st; 
    public Producer(Stack s){ 
    this.st=s; 
    }  public void run(){ 
    char ch; 
    for(int i=0;i <1000;i++){ 
    ch=(char)(Math.random()*26+'A'); 
    st.push(ch); 



    } package MyPackage;
    public class Consumer extends Thread{ 
    private Stack st; 
    public Consumer(Stack s){ 
    this.st=s; 
    }  public void run(){ 
    char ch; 
    for(int i=0;i <1000;i++){ 
    ch=st.pop(); 




    package MyPackage;
    public class MainClass {  public static void main(String[] args) { 
    Stack s=new Stack(); 
    Producer xwh=new Producer(s); 
    Consumer zzh=new Consumer(s);  xwh.start(); 
    zzh.start();  }  }