線程t1在
wait()
被命中後進入死鎖。即使 在t2中有notify()
。代碼正陷入僵局。 沒有得到打印聲明 - 「等待釋放後::::」等待並通知Java中的死鎖情況我可以看到兩個線程競爭獲取顯示器
counterAdd()
。所以,我假設通知會起作用。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
的第二種情況下,在方法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()中的監視器,因此,我認爲通知將起作用。」 –
+0
如果它們是參考不同對象的兩個不同線程,counterAdd()應該總是打印10總是沒有同步關鍵字。 –
相關問題
- 1. Java鎖狀態等待並通知:IllegalMonitorStateException
- 2. 死鎖情況
- 3. 替代等待,並在java中通知
- 4. JDBC + MySQL:在鎖等待或死鎖的情況下重試事務
- 5. 等待並通知
- 6. ManagedObjectContext performBlock(與等待)死鎖
- 7. Java線程等待並通知
- 8. 等待,並通知條件Java
- 9. Java通知並等待異常?
- 10. SQL server 2005死鎖情況
- 11. 等待通知在java
- 12. java.lang.IllegalMonitorStateException等待並通知
- 13. 等待並通知協調
- 14. 等待並通知問題
- 15. 線程中的死鎖情況?
- 16. 如何在這種情況下實現線程等待通知?
- 17. 在java中等待,通知和notifyall?
- 18. 我有已知問題的另一個TIdTCPServer死鎖的情況
- 19. 異步/等待鏈掛起或死鎖
- 20. 異步等待導致死鎖
- 21. 異步等待處理程序死鎖
- 22. Informix - 涉及單表的死鎖情況
- 23. 等待/通知的奇怪java行爲
- 24. 訪問共享資源,鎖定解鎖或等待通知
- 25. TransactionScope優先級(擺脫死鎖情況)
- 26. Java等待/通知不起作用
- 27. Java同步對象,等待和通知
- 28. Java - 無法通知等待線程?
- 29. 等待和在Java通知概念
- 30. Java:等待線程異常通知
不要對沒有引用的文本使用引號格式。 – EJP