IllegalMonitorStateException 当使用 .class 作为监视器而 "this" 不是
IllegalMonitorStateException when use .class as monitor while "this" not
我刚写了一个程序,用两个线程轮流打印0-99。我使用同步块来完成这项工作。在我的代码中,当我使用“this”作为同步监视器时,程序运行良好。但是当我使用“.class”作为同步监视器时,我得到了IllegalMonitorStateException
。谁能告诉我发生了什么事?
这是我的代码,运行良好
public class WaitTest implements Runnable {
private int n = 0;
@Override
public void run() {
while (true) {
synchronized (this){
notify();
if (n < 100) {
System.out.println(Thread.currentThread().getName() + " " + Integer.toString(n));
++n;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else
break;
}
}
}
public static void main(String[] args) {
WaitTest waitTest = new WaitTest();
Thread threadA = new Thread(waitTest);
Thread threadB = new Thread(waitTest);
threadA.start();
threadB.start();
}
}
这是我遇到异常的代码
public class WaitTest implements Runnable {
private int n = 0;
@Override
public void run() {
while (true) {
synchronized (WaitTest.class){
notify();
if (n < 100) {
System.out.println(Thread.currentThread().getName() + " " + Integer.toString(n));
++n;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else
break;
}
}
}
public static void main(String[] args) {
WaitTest waitTest = new WaitTest();
Thread threadA = new Thread(waitTest);
Thread threadB = new Thread(waitTest);
threadA.start();
threadB.start();
}
}
它们之间唯一的区别是synchronized
后大括号中的内容
In my code, when I use "this" as synchronized monitor, the program
works well.
这是因为当您调用方法 wait()
和 notify()
时,您是从实例 return by this
隐式调用它们,这与您的同步子句(即 synchronized (this)
)。
But when I use ".class" as synchronized monitor, I got
IllegalMonitorStateException.
发生这种情况是因为您在 WaitTest.class
class 上进行了同步,但您再次为 this
实例调用了 wait
和 notify
方法。因此,为避免该异常,将 wait(); and notify();
的调用分别更改为 WaitTest.class.wait();
和 WaitTest.class.notify();
。
固定代码:
public class WaitTest implements Runnable {
private int n = 0;
@Override
public void run() {
while (true) {
synchronized (WaitTest.class){
WaitTest.class.notify();
if (n < 100) {
System.out.println(Thread.currentThread().getName() + " " + Integer.toString(n));
++n;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
WaitTest.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else
break;
}
}
}
public static void main(String[] args) {
WaitTest waitTest = new WaitTest();
Thread threadA = new Thread(waitTest);
Thread threadB = new Thread(waitTest);
threadA.start();
threadB.start();
}
}
我刚写了一个程序,用两个线程轮流打印0-99。我使用同步块来完成这项工作。在我的代码中,当我使用“this”作为同步监视器时,程序运行良好。但是当我使用“.class”作为同步监视器时,我得到了IllegalMonitorStateException
。谁能告诉我发生了什么事?
这是我的代码,运行良好
public class WaitTest implements Runnable {
private int n = 0;
@Override
public void run() {
while (true) {
synchronized (this){
notify();
if (n < 100) {
System.out.println(Thread.currentThread().getName() + " " + Integer.toString(n));
++n;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else
break;
}
}
}
public static void main(String[] args) {
WaitTest waitTest = new WaitTest();
Thread threadA = new Thread(waitTest);
Thread threadB = new Thread(waitTest);
threadA.start();
threadB.start();
}
}
这是我遇到异常的代码
public class WaitTest implements Runnable {
private int n = 0;
@Override
public void run() {
while (true) {
synchronized (WaitTest.class){
notify();
if (n < 100) {
System.out.println(Thread.currentThread().getName() + " " + Integer.toString(n));
++n;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else
break;
}
}
}
public static void main(String[] args) {
WaitTest waitTest = new WaitTest();
Thread threadA = new Thread(waitTest);
Thread threadB = new Thread(waitTest);
threadA.start();
threadB.start();
}
}
它们之间唯一的区别是synchronized
后大括号中的内容In my code, when I use "this" as synchronized monitor, the program works well.
这是因为当您调用方法 wait()
和 notify()
时,您是从实例 return by this
隐式调用它们,这与您的同步子句(即 synchronized (this)
)。
But when I use ".class" as synchronized monitor, I got IllegalMonitorStateException.
发生这种情况是因为您在 WaitTest.class
class 上进行了同步,但您再次为 this
实例调用了 wait
和 notify
方法。因此,为避免该异常,将 wait(); and notify();
的调用分别更改为 WaitTest.class.wait();
和 WaitTest.class.notify();
。
固定代码:
public class WaitTest implements Runnable {
private int n = 0;
@Override
public void run() {
while (true) {
synchronized (WaitTest.class){
WaitTest.class.notify();
if (n < 100) {
System.out.println(Thread.currentThread().getName() + " " + Integer.toString(n));
++n;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
WaitTest.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else
break;
}
}
}
public static void main(String[] args) {
WaitTest waitTest = new WaitTest();
Thread threadA = new Thread(waitTest);
Thread threadB = new Thread(waitTest);
threadA.start();
threadB.start();
}
}