java (线程的同步与并发)
一道作业编写一个堆栈MyStack类,栈的大小为10个元素,每个元素可以放一个字符。再编写两个线程,一个线程PushChar调用MyStack的入栈方法,往MyStack...
一道作业编写一个堆栈MyStack类,栈的大小为10个元素,每个元素可以放一个字符。再编写两个线程,一个线程PushChar调用MyStack的入栈方法,往MyStack的实例中压入20个字符,另一个线程PopChar调用MyStack的出栈方法,取出压入的20个字符。要求实现两个线程的同步和交互。做入栈操作发现栈满时等待出栈;出栈操作发现栈空时等待入栈。
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 ("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();
}
}
Exception in thread "Thread-1" java.lang.NoSuchMethodError: Mystack.pop()C
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 ("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();
}
}
Exception in thread "Thread-1" java.lang.NoSuchMethodError: Mystack.pop()C
push 部分没有问题,但是pop就弄不出来了 展开
1个回答
展开全部
试试我改过的这个
class Mystack
{
int index=0;
private char[]date=new char [10];
public void push(char c)
{
synchronized(this)
{
if(index<10)
{
date[index]=c;
index++;
if(index==10)
notify();
}
else
{
try
{
System.out.println ("push wait");
wait();
}
catch(Exception e)
{
e.printStackTrace();
System.err.println(e);
}
}
}
}
public void pop()
{
synchronized(this)
{
if(index!=0)
{
if(index>9)
index--;
System.out.print ("B:pop");
System.out.println (date[index]);
index--;
if(index==0)
notify();
}
else
{
try
{
System.out.println ("pop wait");
wait();
}
catch(Exception e)
{
e.printStackTrace();
System.err.println (e);
}
}
}
}
}
class PushChar extends Thread
{
Mystack s;
char c;
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;
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
{
int index=0;
private char[]date=new char [10];
public void push(char c)
{
synchronized(this)
{
if(index<10)
{
date[index]=c;
index++;
if(index==10)
notify();
}
else
{
try
{
System.out.println ("push wait");
wait();
}
catch(Exception e)
{
e.printStackTrace();
System.err.println(e);
}
}
}
}
public void pop()
{
synchronized(this)
{
if(index!=0)
{
if(index>9)
index--;
System.out.print ("B:pop");
System.out.println (date[index]);
index--;
if(index==0)
notify();
}
else
{
try
{
System.out.println ("pop wait");
wait();
}
catch(Exception e)
{
e.printStackTrace();
System.err.println (e);
}
}
}
}
}
class PushChar extends Thread
{
Mystack s;
char c;
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;
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();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询