有如下的代码段,当编译和运行时,下列各选项中说法正确的是( )。 public class Z { public static void main(String args[]) { new Z()

admin2010-07-28  34

问题 有如下的代码段,当编译和运行时,下列各选项中说法正确的是(    )。    public class Z    {       public static void main(String args[])       {          new Z();       }       Z()       {         Z aliasl=this;         Z alias2=this;         synchronized(aliasl)  {         try{             alias2.walt();             System.out.println("DONE WAITING");         }         catch(InterruptedException e)  {             System.out.println("INTERR UPTED");         }         catch (Exception e)  {             System.out.println("OTHER EXCEPTION");         }         finally{             System.out.println("FINALLY");         }       }        System.out.println("ALL DONE");       }    }

选项 A、应用程序编译正常,但是不打印任何数据
B、应用程序编译正常,并打印数据“DONE WAITING”
C、应用程序编译正常,并打印数据“FINALLY”
D、应用程序编译正常,并打印数据“ALL DONE”

答案1

解析 在Java中,每一个对象都有锁。任何时候,该锁都至多由一个线程控制。由于 aliasl与alias2指向同一对象Z,在执行第11行前,线程拥有对象z的锁。在执行完第11行后,该线程释放了对象Z的锁,进入等待池。但此后没有线程调用对象Z的notify()和 notifyAll()方法,所以该进程一直处于等待状态,没有输出。
转载请注明原文地址:https://kaotiyun.com/show/AQ9p777K
0

最新回复(0)