当前位置:编程学习 > JSP >>

大家想想...(答:难道是编译器的问题?)

答案:###########################################################
问题:
东北活雷锋 于 2001-12-13 17:12:56 加贴在 Java认证 ←返回版面    
Question 22)

Which statement is true of the following code?

public class Agg{
public static void main(String argv[]){
    Agg a = new Agg();
    a.go();
    }
    public void go(){
        DSRoss ds1 = new DSRoss("one");
        ds1.start();
    }
}

class DSRoss extends Thread{
private String sTname="";
DSRoss(String s){
    sTname = s;
}
public void run(){
    notwait();
    System.out.println("finished");
}
public void notwait(){
    while(true){
        try{
             System.out.println("waiting");
             wait();
            }catch(InterruptedException ie){}
        System.out.println(sTname);
        notifyAll();
        }
    }
    
}

1) It will cause a compile time error
2) Compilation and output of "waiting"
3) Compilation and output of "waiting" followed by "finished"
4) Runtime error, an exception will be thrown


上面的 代码我用jdk编译报错
可是我用 Ibm visualage编译没有问题
为什么呢?

#############################################################


在wait方法中有这样一段话:
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.

程序中当前线索调用了自身(the object)的wait方法,而且当前线索是不是the owner of the object's monitor呢?

再结合一下notify()方法中的一段话:
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:


By executing a synchronized instance method of that object.
By executing the body of a synchronized statement that synchronizes on the object.
For objects of type Class, by executing a synchronized static method of that class.


Only one thread at a time can own an object's monitor.

Throws:

IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.

给大家这个思路,好好想想吧,:)  

上一个:文档位置...
下一个:方法...

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,