帮我看看这个java小程序。

来源:百度知道 编辑:UC知道 时间:2024/05/16 12:54:09
刚刚学习java不久,昨晚编个小程序,编译时老是说两个wait();函数有问题,可我又不知道怎么改。谁帮我改改?在class Q中
class Q
{
private String name="陈琼";
private String sex="男";
boolean bFull=false;
public synchronized void put(String name,String sex)
{
if(bFull)
wait();throw InterruptedException;
this.name=name;
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
this.sex=sex;
bFull=true;
notify();
}
public synchronized void get()
{
if(!bFull)
wait();throw InterruptedException;
System.out.println(name+"----->"+sex);
bFull=false;
notify();
}
}
class Producer implements Runnable
{
Q q=null;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
whil

当然有问题了,你一wait()线程就挂起了,代码就不再往下执行了。
Q q=new Q();
new Thread(new Producer(q)).start();
这样线程就挂在wait()这一句后面等待了
你必须执行
q.notify();
线程才会继续执行的。

你说说,你写这个wait()的目的是什么?

你的目的是想实现线程的同步把,可惜用错拉.
上面说的对.