gpt4 book ai didi

java - Java中的等待和通知死锁情况

转载 作者:行者123 更新时间:2023-11-30 06:53:22 25 4
gpt4 key购买 nike

  1. 线程 t1 在 wait() 被命中后将进入死锁。虽然t2 中有 notify()。代码陷入僵局。未收到打印语句 - “等待释放后::::”

  2. 我可以看到两个线程在 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);

    }
    }

最佳答案

您正在不同的对象上进行同步。第一种情况是在对象 t1 上,第二种情况是在 t2 上,在 andNotify4 上的方法 counterAdd 中。为了始终锁定 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);
}
}

关于java - Java中的等待和通知死锁情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com