スレッドで10秒まてからnotifyしていますが、
waitした後、3秒でタイムアウトで抜ける想定ですが、
想定通りの通りの動きををしてくれません。どなたか理由をご存知でしょうか。
ClassA.java
lang
1public class ClassA { 2 public static void main(String[] args) { 3 final ClassB body = new ClassB(); 4 body.start(); 5 6 synchronized (body) { 7 try { 8 System.out.println("wait start"); 9 body.wait(3000); 10 System.out.println("wait end"); 11 } catch (InterruptedException e1) { 12 } 13 } 14 } 15}
ClassB.java
lang
1public class ClassB extends Thread { 2 public void run() { 3 synchronized (this) { 4 try { 5 System.out.println("sleep start"); 6 Thread.sleep(10000); 7 } catch (InterruptedException e) { 8 } 9 System.out.println("notify"); 10 notify(); 11 } 12 } 13}
[実行結果]
wait start
sleep start
notify
wait end
となってしまいます。
本当は、notifyの前にwait endを想定していますが、wait(3000)が効いていません。
どなたか、理由をご存じでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/07 22:51