wait()
にヒットしたスレッドt1はデッドロック状態になります。 でも、t2にはnotify()
があります。コードがデッドロックに陥っています。 Printステートメントが取得されない - 「待機が解放された後::::」Javaのデッドロック状況を待って通知するcounterAdd()
でモニターを取得するために競合する両方のスレッドがわかります。したがって、私はnotifyが動作すると仮定しました。package com.java.thread.practice; public class WaitAndNotify4 { int counter = 0; /* CounterAdd() is to be accessed by both t1 and t2. If not synchronized not giving consistent output. */ synchronized int counterAdd(){ return counter++; } public static void main(String[] args) throws InterruptedException{ // Creating method to call the threads. WaitAndNotify4 andNotify4 = new WaitAndNotify4(); andNotify4.testRaceCondition(); } private void testRaceCondition() throws InterruptedException { // Thread t1 created and launched. Thread t1 = new Thread(new Runnable(){ @Override public void run() { for(int i=0; i<5; i++){ synchronized(this){ if(i== 1){ System.out.println("Calling wait after count 1"); try { // Assuming that this wait will be resumed by notify in t2. wait(); System.out.println("After wait is released :::: "); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } counterAdd(); } } }); Thread t2 = new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub for(int i=0; i<5; i++){ if(i==2){ synchronized(this){ System.out.println("Before releasing the counter :::::"); notify(); System.out.println("After releasing the counter :::::"); } } counterAdd(); } } }); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(" Sum value is found as ::::: "+counter); } }
1
A
答えて
1
あなたは、異なるオブジェクトに同期させています。オブジェクトt1
の最初のケースでは、t2
の2番目に、方法counterAdd
のandNotify4
にあります。 andNotify4
にロックをかけるには、そのようなことをする必要があります。
public class Main {
private int counter = 0;
synchronized int counterAdd() {
return counter++;
}
public static void main(String[] args) throws InterruptedException {
Main andNotify4 = new Main();
andNotify4.testRaceCondition();
}
private void testRaceCondition() throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
synchronized (Main.this) {
if (i == 1) {
System.out.println("Calling wait after count 1");
try {
Main.this.wait();
System.out.println("After wait is released :::: ");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
counterAdd();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
if (i == 2) {
synchronized (Main.this) {
System.out.println("Before releasing the counter :::::");
Main.this.notify();
System.out.println("After releasing the counter :::::");
}
}
counterAdd();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(" Sum value is found as ::::: " + counter);
}
}
+0
ありがとうございました。それは問題を解決し、概念もクリアします。 もしあなたも部分的にいくつかの光を投げることができます。 " counterAdd()で両方のスレッドが競合してモニタを取得していることがわかります。したがって、notifyは動作すると仮定しました。 –
+0
異なるオブジェクトを参照する2つの異なるスレッドである場合counterAdd()はsynchronizedキーワードなしで常に10を出力する必要があります。 –
関連する問題
- 1. デッドロックの待機/通知
- 2. 別のTIdTCPServerのデッドロック状況
- 3. SQLサーバー2005のデッドロック状況
- 4. 通知の進捗状況を表示
- 5. VBScriptスクリプトの進捗状況の通知
- 6. 進捗状況通知ECMAScript約束
- 7. cURL - アップロードの進行状況を通知する
- 8. サービスの進行状況をアクティビティに通知するには
- 9. 進行状況をユーザーに通知するには
- 10. スレッド:()を通知し、(待つ)javaの
- 11. Anylogic:交通状況
- 12. 状況が成功するとAjaxユーザー通知
- 13. 完了した注文状況の電子メール通知のカスタムメッセージ
- 14. Javaロック条件待ちと通知:IllegalMonitorStateException
- 15. シリアルリンクで読み込みを待っているデータの通知
- 16. バックグラウンドフェッチ要求と通知がすべての状況で機能しない
- 17. 招待するユーザー通知
- 18. 進捗状況を他のクラスから継続的に通知する
- 19. 詳細な状況を追跡する投稿後のGCM通知
- 20. 待機/通知で保証されたデッドロックを書き込む方法
- 21. JavaメールでJProgressBarを使用する(transport.send()の後の進捗状況を知る)
- 22. Javaインターフェイスのデザインパターンの状況
- 23. PayPal支払い通知を待っているASP.NET
- 24. 通話状態の通知
- 25. ハードJava配列の状況
- 26. Javaスレッドのダンプ表示デッドロックの症状
- 27. 待っているすべてのgoルーチンを通知するには?
- 28. マルチスレッド待ち()通知()
- 29. 友人の通知に招待状が表示されない
- 30. Javaは()、のnotifyAll()を通知し、待ちます()メソッド
引用符で囲まれていないテキストには引用形式を使用しないでください。 – EJP