• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网 ###########################################################
问题:
东北活雷锋 于 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.

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

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网