Topic: 面试题目 线程的分析 请高手帮忙

  Print this page

1.面试题目 线程的分析 请高手帮忙 Copy to clipboard
Posted by: athen
Posted on: 2005-05-02 23:49

public class Z{
public static void main(String [] args){
new Z();
}

Z(){
Z z1 = this;
Z z2 = this;
System.out.println("begin !");
synchronized (z1){
try{
z2.wait();
System.out.println("done waiting !");
}catch(InterruptedException e){
System.out.println("interrupted !");
}catch(Exception e){
System.out.println("other exception !");
}finally{
System.out.println("finally !");
}
}
System.out.println("all done !");
}
}
请分析可能出现的结果 ,并分析原因 :一道面试题目

2.Re:面试题目 线程的分析 请高手帮忙 [Re: athen] Copy to clipboard
Posted by: jigsaw
Posted on: 2005-05-06 01:18

我猜。。。
程序会输出:
begin!
然后就没有了

3.Re:面试题目 线程的分析 请高手帮忙 [Re: athen] Copy to clipboard
Posted by: amigobot
Posted on: 2005-05-06 07:01

just one thread, so it's single thread problem. the wait will stop its execution.

4.Re:面试题目 线程的分析 请高手帮忙 [Re: athen] Copy to clipboard
Posted by: scorcer
Posted on: 2005-05-10 10:20

begin!

5.Re:面试题目 线程的分析 请高手帮忙 [Re: athen] Copy to clipboard
Posted by: flyingswor
Posted on: 2005-05-10 14:41

I guess from Z z1=this and Z z2=this,
we can got z2=z1=this.currentThread;
when waiting, and no another Thread to await it, It will involve into the dead loop;

6.Re:面试题目 线程的分析 请高手帮忙 [Re: athen] Copy to clipboard
Posted by: flyingswor
Posted on: 2005-05-10 14:46

public class Z{
public static void main(String [] args){
new Z();
}

Z(){
Z z1 = this;
Z z2 = this;
System.out.println("begin !");
synchronized (z1){
try{
z2.wait(1000);////////change the waiting time
System.out.println("done waiting !");
}catch(InterruptedException e){
System.out.println("interrupted !");
}catch(Exception e){
System.out.println("other exception !");
}finally{
System.out.println("finally !");
}
}
System.out.println("all done !");
}
}

7.Re:面试题目 线程的分析 请高手帮忙 [Re: athen] Copy to clipboard
Posted by: 一枝梅
Posted on: 2005-05-13 10:47

2.在Z2.wait();里加上参数1的话,为什么显示如下:
begin !
done waiting !
finally !
all done !
而interrupted !other exception !为什么不显示?


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923