package org.it315;
public class Q
{
private String name="陈琼";
private String sex="女";
boolean bFull=false;
public synchronized void put(String name,String sex)
{
if(bFull)
wait();
this.name=name;
this.sex=sex;
bFull=true;
notify();
}
public synchronized void get()
{
if(!bFull)
wait();
System.out.println(this.name+"---------"+this.sex);
bFull=false;
notify();
}
}--------------------配置: <默认>--------------------
F:\5\Q.java:10: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
wait();
^
F:\5\Q.java:19: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
wait();
^
2 errors处理已完成。
public class Q
{
private String name="陈琼";
private String sex="女";
boolean bFull=false;
public synchronized void put(String name,String sex)
{
if(bFull)
wait();
this.name=name;
this.sex=sex;
bFull=true;
notify();
}
public synchronized void get()
{
if(!bFull)
wait();
System.out.println(this.name+"---------"+this.sex);
bFull=false;
notify();
}
}--------------------配置: <默认>--------------------
F:\5\Q.java:10: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
wait();
^
F:\5\Q.java:19: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
wait();
^
2 errors处理已完成。
在JAVA中,除了RuntimeExcepton外,其它的可能抛出异常的方法都必须进行异常处理。上面的程序可以在
put(String name,String sex) 和 public synchronized void get() 简单的加上throws InterruptedException