您正在从中获取“非法监视器状态异常”
available.wait()
由“可用”对象引用引用。
-
通过执行该对象的同步实例方法。
-
通过执行在对象上同步的同步块的主体。
-
每个场景的简单示例代码。对于每种类型,所有三个代码段都是独立的类,只需复制代码并运行它即可。我在代码中添加了大量注释,以解释每种情况下发生的情况。如果对你来说评论太多。只需删除它们,使代码更简洁。
另外,首先阅读main()方法中的代码,了解threadOne和threadTwo。
-
通过执行该对象的同步实例方法。
import static java.lang.System.out;
public class SynchronizedInstanceMethodClass {
synchronized void synchronizedInstanceMethod() { // threadOne acquire the monitor for "this" and continue.
try {
out.println("EVENT #1 threadOne is about to strat waiting on the "
+"monitor it already has - [\"this\"]....");
this.wait(); // The threadOne already have the monitor for "this",
// just release the monitor and go and wait threadOne.
out.println("EVENT #3 Notify received and continue execution...");
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
synchronized void notifierForAllThreads() { // threadTwo acquire the monitor for "this",
// which was released by threadOne when it went to waiting and contine.
out.println("EVENT #2 threadTwo is about to notify all threads(including threadOne) "
+" waiting on the monitor of -[\"this\"]....");
this.notifyAll(); // threadTwo who owns the monitor on "this" notifies all
// threads waiting on "this" and releases the monitor
}
public static void main(String [] args) {
SynchronizedInstanceMethodClass mc = new SynchronizedInstanceMethodClass();
Thread threadOne = new Thread(() -> {mc.synchronizedInstanceMethod();});
Thread threadTwo = new Thread(() -> {mc.notifierForAllThreads();});
threadOne.start(); // Start the waiting of Thread one
threadTwo.start(); // Notify the waiting threadOne
}
}
-
import static java.lang.System.out;
public class SynchronizedBlockClass {
void synchronizedBlockInstanceMethod() {
synchronized (this) { // threadOne acquire the monitor for "this" and continue.
try {
out.println("EVENT #1 threadOne is about to strat waiting on the "
+"monitor it already has - [\"this\"]....");
this.wait(); // The threadOne already have the monitor for "this",
// just release the monitor and go and wait threadOne.
out.println("EVENT #3 Notify received and continue execution...");
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
void synchronizedBlockNotifierForAllThreads() {
synchronized (this) { // threadTwo acquire the monitor for "this",
// which was released by threadOne when it went to waiting and continue.
out.println("EVENT #2 threadTwo is about to notify all threads(including threadOne) "
+" waiting on the monitor of -[\"this\"]....");
this.notifyAll(); // threadTwo who owns the monitor on "this" notifies all
// threads waiting on "this" and releases the monitor
}
}
public static void main(String [] args) {
SynchronizedBlockClass mc = new SynchronizedBlockClass();
Thread threadOne = new Thread(() -> {mc.synchronizedBlockInstanceMethod();});
Thread threadTwo = new Thread(() -> {mc.synchronizedBlockNotifierForAllThreads();});
threadOne.start(); // Start the waiting of Thread one
threadTwo.start(); // Notify the waiting threadOne
}
}
-
对于类类型的对象,执行该类的同步静态方法。
import static java.lang.System.out;
public class StaticClassReferenceClass {
void synchronizedBlockInstanceMethod() {
synchronized (StaticClassReferenceClass.class) { // threadOne acquire the monitor for class literal and continue.
try {
out.println("EVENT #1 threadOne is about to strat waiting on the "
+"monitor it already has - [StaticClassReferenceClass.class]....");
StaticClassReferenceClass.class.wait(); // The threadOne already have the monitor for the class literal,
// So it just release the monitor and go and wait.
out.println("EVENT #3 Notify received and continue execution...");
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
void synchronizedBlockNotifierForAllThreads() {
synchronized (StaticClassReferenceClass.class) { // threadTwo acquire the monitor for the class literal,
// which was released by threadOne when it went to waiting.
out.println("EVENT #2 threadTwo is about to notify all threads(including threadOne) "
+" waiting on the monitor of -[StaticClassReferenceClass.class]....");
StaticClassReferenceClass.class.notifyAll(); // threadTwo who owns the monitor on the class literal notifies all
// threads waiting on it and releases the monitor
}
}
public static void main(String [] args) {
StaticClassReferenceClass mc = new StaticClassReferenceClass();
Thread threadOne = new Thread(() -> {mc.synchronizedBlockInstanceMethod();});
Thread threadTwo = new Thread(() -> {mc.synchronizedBlockNotifierForAllThreads();});
threadOne.start(); // Start the waiting of Thread one
threadTwo.start(); // Notify the waiting threadOne
}
}