欢迎光临
我们一直在努力

大家想想…(答:难道是编译器的问题?)-JSP教程,资料/其它

建站超值云服务器,限时71元/月

###########################################################
问题:
东北活雷锋 于 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 objects monitor.

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

再结合一下notify()方法中的一段话:
this method should only be called by a thread that is the owner of this objects monitor. a thread becomes the owner of the objects 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 objects monitor.

throws:

illegalmonitorstateexception – if the current thread is not the owner of this objects monitor.

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 大家想想…(答:难道是编译器的问题?)-JSP教程,资料/其它
分享到: 更多 (0)