java - 为什么我的java同步方法会发生这个错误?

标签 java multithreading synchronized

我是一个java新手,正在学习java中线程与wait()notify()的通信,并且发生了一些愚蠢的事情:
事情是这样的:我想通过多线程不断设置人类并获取人类,这意味着结果是( jack 男,玛丽女, jack 男,玛丽女……)
这是我的代码:

class Human {
  private String name;
  private String sex;
  private boolean b = false;

  public synchronized void set(String name, String sex) {
    if (b) {
        try {
            this.wait();
        } catch (InterruptedException e) {}
    } 
    this.name = name;
    this.sex = sex;
    b = true;
    this.notify();         
  }      
  public synchronized void get() {
    if (!b) {
        try {
            this.wait();
        } catch (InterruptedException e) {}
    }  
    System.out.println(name+"  "+sex);
    b = false;
    this.notify();        
  }
}
class SetHuman implements Runnable {
  private Human h;
  SetHuman(Human h) {
    this.h = h;
  }
  public void run() {     
    int x = 0;
    while(true) {           
        if (x==0) {
            h.set("Jack","male");
        }  else {
            h.set("Mary","female");
        }
        x = (x+1)%2;
    }
  }
}
class GetHuman implements Runnable {
  private Human h;
  GetHuman(Human h) {
    this.h = h;
  }
  public void run() {
    while (true) {
        h.get();
    }
  }
}

class HumanDemo  {
  public static void main(String[]args) {
    Human h = new Human();
    SetHuman sh = new SetHuman(h);
    GetHuman gh = new GetHuman(h);
    
    Thread t1 = new Thread(sh);
    Thread t2 = new Thread(gh);
    
    t1.start();
    t2.start();
  }
}

当我运行 HumanDemo 时,它起作用了:result

然后,我在同步函数set()和get()中添加了else判断,就出现了这样的情况:

public synchronized void set(String name, String sex) {
    if (b) {
        try {
            this.wait();
        } catch (InterruptedException e) {}
    } else {
        this.name = name;
        this.sex = sex;
        b = true;
        this.notify();
     }
}      
public synchronized void get() {
    if (!b) {
        try {
            this.wait();
        } catch (InterruptedException e) {}
    }  else {
        System.out.println(name+"  "+sex);
        b = false;
        this.notify();
    }
}

new result

这是为什么呢?有人请告诉我为什么吗?谢谢^-^!

最佳答案

在您的第一个工作示例中,您的 set/get() 方法是互斥的。 this.wait() 将迫使它们等待对方才能完成工作。

在第二个示例中,您用 else 打破了 this ,因为无法保证释放锁后哪个线程将获得“this”上的锁定。这样,任意数量的“set()”可能会丢失到等待状态,而永远不会将其值设置为“this.name”和“this.sex”。

示例(gt=获取线程 st=设置线程):

主方法启动线程st

st: h.set(" jack ","男"); b 为 false -> this.name = "Jack"; b=真; (现在不能保证 st 是否会再次执行,或者 gt 是否已创建,并将通过同步 get() 方法获取“this”上的锁。这次让 st 获取锁。)

?main-method 启动线程 gt? (可能会稍后)

st: h.set("玛丽","女"); b 为 true -> this.wait(); (现在 st 正在等待,直到有人释放对“this”的锁定。由于 this.name 和 this.sex 在 else 语句中设置,因此它永远不会设置其当前值,并且带有“Marry”和“female”的调用将丢失.所以下一个gt将执行。)

?main-method 启动线程 gt? (可能已经发生了)

gt: b 为 true -> System.out.println(name+""+sex); b = false ...(在方法结束时,gt 将释放“this”上的锁,现在 st 将离开等待状态并尝试获取“this”的锁。gt 也在尝试获取“this”的锁再次强调。同样不能保证哪个线程将获得锁并且现在可以执行。)

长话短说:

由于 set 方法中的 else ,您将丢弃“随机”数量的 set 调用(因此不太可能出现交替顺序)。如果 set 方法中没有 else ,它应该可以工作。尽管这会浪费对 get 的方法调用,但它会进入等待状态以返回而没有完成任何工作。

关于java - 为什么我的java同步方法会发生这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513284/

相关文章:

java - 对于可变引用字段中的不可变类型,使用 volatile 并本地缓存还是同步?

java - Java编译器不应该强制同步吗?

java - 表关系 多对多没有 sql 中的中间表?

java - 如何从 nattable 单元格中获取鼠标位置以进行 rcptt 测试?

java - Install4j - 如何获取安装程序的版本?

winforms - 类实例的新线程 (C#)

python C扩展: multithreading and random numbers

java - 如何从方法之前和之后带有参数的函数在 Dialect 上注册函数?

c# - 程序崩溃后如何挂起所有线程?

Java同步列表for循环