本帖最后由 洪建超 于 2013-2-27 23:11 编辑
public class Exercise {
public static void main(String[] args) {
Exercise ex=new Exercise();
ex.go();
}
public void go(){
MyThread mt=new MyThread("one");
mt.start();
}
}
class MyThread extends Thread{
private String name="";
MyThread(String s){
name=s;
}
public void run(){
notwait();
System.out.println("finished");
}
public void notwait(){
while(true){
try{
System.out.println("waiting");
wait();
}
catch(InterruptedException e){}
System.out.println(name);
}
}
}
结果为
waiting
以下是运行异常显示的东东
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at mypackage.MyThread.notwait(Exercise.java:34)
at mypackage.MyThread.run(Exercise.java:24)
|