编写一个堆栈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();
}
}
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();
}
}
解决方案 »
- 如何用replace把/换成//如:Str.replaceAll("\","\\")
- 关于this
- 4棵树,怎么栽让他们两棵树之间的距离相等(IQ题)有方案者有分!
- java面板的问题
- 求助啊!请大家给出能编译过去的代码,越短越好
- java语法问题
- applet在IE中不能正确运行
- Struts可以利用外部资源文件来实现国际化,那么一个标准的带有main()方法的JAVA应用程序如何实现利用外部资源文件的国际化?
- 求 Java 2 图形设计卷Ⅱ:SWING 电子书
- 我想在linux下使用java,已经安装jdk了,请问如何设置环境变量?
- 请问专家一个关于SocketTimedoutException的问题
- 各位,帮我看看这个错误,是怎么回事.如何解决,谢谢...
如: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()内代码你自己相应地修改一下.
发表于: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