编写一个堆栈MyStack类,栈的大小为10个元素,每个元素可以放一个字符。再编写两个线程,一个线程PushChar调用MyStack的入栈方法,往MyStack的实例中压入20个字符,另一个线程PopChar调用MyStack的出栈方法,取出压入的20个字符。要求实现两个线程的同步和交互。做入栈操作发现栈满时等待出栈;出栈操作发现栈空时等待入栈。Push部门是可以运行的,但是pop部分怎么都出不来 
class Mystack
{
private int index=0;
private char[]date=new char [10];

public void push(char c)
{
  synchronized(this)
  {
   if(index<10)
  {
   date[index]=c;
index++;
  }
  else
  {
   System.out.println ("push wait");
   try
   {
   wait();
   }
   catch(Exception e)
   {
   e.printStackTrace();
   System.err.println(e);
   } 
   notify();
  }
  }

  
 



}

public  void pop()
{
synchronized(this)
{
if(index!=0)
{
System.out.print ("B:pop");
         System.out.print (date[index]) ;
         index--;

}
else
{
System.out.println ("empty stack,pop wait");
try
{
wait();
}
catch(Exception e)
{
e.printStackTrace();
System.err.println (e);
}

notify();
}
}


}


class PushChar extends Thread
 {
 Mystack s;
     char c;
 
public PushChar(Mystack s)
{
this.s=s;
}



public void run()
{
System.out.println ("start push");
for (int i=0; i<20; i++)
{
    c=(char)(Math.random()*26+'A');
    s.push(c);
    System.out.println("A:push "+c);
      
     }
  }
 
}class PopChar extends Thread
{
Mystack s;
char c;

public PopChar(Mystack s)
{
this.s=s;
}

public void run()
{
System.out.println ("start pop");
for(int j=0;j<20;j++)

s.pop();
}
}
}public class Test
{
public static void main (String[] args) 
    {
     Mystack s=new Mystack();
     PushChar a=new PushChar(s);
     PopChar b=new PopChar(s);
        a.start();
     b.start();
    }
}

解决方案 »

  1.   

    答:push()与pop()内部代码结构不好.
    如:push()内代码要改成:synchronized(this) 
      { 
      while(index >=10) 
      { 
      System.out.println ("push wait"); 
      try 
      { 
      wait(); 
      } 
      catch(Exception e) 
      { 
      e.printStackTrace(); 
      System.err.println(e); 
      }  
      } //while
     date[index]=c; 
     index++; 
      
      notify(); 
      } pop()内代码你自己相应地修改一下.
      

  2.   

    但是push的那部分是可以执行的,没有什么问题,为什么加上pop就与问题了,你能把代码贴下么
      

  3.   


     发表于:2008-12-21 22:40:09 楼主 
    编写一个堆栈MyStack类,栈的大小为10个元素,每个元素可以放一个字符。再编写两个线程,一个线程PushChar调用MyStack的入栈方法,往MyStack的实例中压入20个字符,另一个线程PopChar调用MyStack的出栈方法,取出压入的20个字符。要求实现两个线程的同步和交互。做入栈操作发现栈满时等待出栈;出栈操作发现栈空时等待入栈。 Push部门是可以运行的,但是pop部分怎么都出不来 
    class Mystack 

    private int index=-1; 
    private char[]date=new char [10]; public void push(char c) 

      synchronized(this) 
      { 
     while(index>=9){
      System.out.println ("push wait"); 
      try 
      { 
      wait(); 
      } 
      catch(Exception e) 
      { 
      e.printStackTrace(); 
      System.err.println(e); 
      }   }//while   date[++index]=c; 
       notifyAll(); 
       
      }//syn 
    } //pushpublic  void pop() 

    synchronized(this) 

     while(index<0){
     System.out.println ("empty stack,pop wait"); 
    try 

    wait(); 

    catch(Exception e) 

    e.printStackTrace(); 
    System.err.println (e); 
    }  }//while
     
     System.out.print ("B:pop"); 
     System.out.print (date[index]) ; 
     index--; 
     notifyAll(); } //syn

    }//pop