线程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