gpt4 book ai didi

java通知所有: illegalMonitorStateException

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:58 25 4
gpt4 key购买 nike

我想模拟一个妈妈叫三个 child 吃饭,这是我的代码:

class child extends Thread{
String name;
mother mom;
public child(mother mon, String name){
this.name = name;
this.mom = mom;
}

public void run(){
System.out.println(name+" is hungry.");
while (!mom.finished){
try {
wait();
}catch(InterruptedException e){}
}
washhands();
}

public synchronized void washhands(){
System.out.println(name+" washed hands.");
notifyAll();
}
}

class mother{
boolean finished;

public mother(){
finished = false;
}
public void cook(){
System.out.println("Mom started cooking.");
try{
Thread.sleep(200);
}catch(InterruptedException e){};
finished = true;
System.out.println("Mom finished cooking.");
notifyAll();
}
}

public class work{
public static void main(String[] args){
mother mom = new mother();
child Alex = new child(mom, "Alex");
child James = new child(mom, "James");
child Tod = new child(mom, "Tod");

Alex.start();
James.start();
Tod.start();
mom.cook();
}
}

所以我在类 mother 的 notifyAll() 行处得到了“线程“main”java.lang.IllegalMonitorStateException 中的异常”。我不知道会发生什么,有人可以帮助我吗?

最佳答案

考虑以下几点。

  1. 您必须使用一个通用对象来保护所有需要互斥的代码块。
  2. 只有当对象的监视器被当前线程锁定时,才能对对象调用 wait/notify。
  3. 要获取监视器,线程必须进入由拥有监视器的对象保护的 block 。监视器由 JVM 内部使用。

除此之外,使用 Java naming conventions. 命名您的类和变量

看下面的代码

synchronized void method1 () {
}

synchronized void method2() {
}

这里method1method2将受到调用它们的对象的监视器的保护。因此,如果我们想在 protected block 内使用相同的监视器,将按如下方式完成。

public void method3() {
synchronized(this) {
}
...
}

现在 method1()method2()method3() 中的 protected block 将受到同一对象监视器的保护。

如果我们需要跨类的保护 block ,那么我们需要在它们之间共享一个公共(public)对象。我在下面的代码中进行了相同的修改。

package filevisitor;

import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

class child extends Thread{
String name;
volatile mother mom;
public child(mother mom, String name){
this.name = name;
this.mom = mom;
}

public void run(){
System.out.println(name+" is hungry.");
while (!mom.finished){
try {
synchronized(work.myLockObject) {
work.myLockObject.wait();
}
}catch(InterruptedException e){}
}
washhands();
}

public void washhands(){
System.out.println(name+" washed hands.");
synchronized(work.myLockObject) {
work.myLockObject.notifyAll();
}
}
}

class mother{
boolean finished;

public mother(){
finished = false;
}
public void cook(){
System.out.println("Mom started cooking.");
try{
Thread.sleep(200);
}catch(InterruptedException e){};
finished = true;
System.out.println("Mom finished cooking.");
synchronized(work.myLockObject) {
work.myLockObject.notifyAll();
}
}
}

public class work{
public static final Object myLockObject = new Object();
public static void main(String[] args){
mother mom = new mother();
child Alex = new child(mom, "Alex");
child James = new child(mom, "James");
child Tod = new child(mom, "Tod");

Alex.start();
James.start();
Tod.start();
mom.cook();
}
}

子类中的 this.mom = mom; 还存在一个小错误。参数名称为 mon,因此此分配没有执行任何操作,导致 NullPointerException

输出

Mom started cooking.
Alex is hungry.
Tod is hungry.
James is hungry.
Mom finished cooking.
Tod washed hands.
James washed hands.
Alex washed hands.

关于java通知所有: illegalMonitorStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574768/

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