多线程wait/notify/notifyAll - IllegalMonitorStateException
一,多个线程获取相同锁资源后的wait/notify/notifyAll操作产生的结果。
【wait / notify 情景】
public class Demo2 {
public volatile static int count = 3;
public static void main(String[] args) {
// 锁对象1
Object object = new Object();
// 锁对象2
Object newObject = new Object();
Thread thread1 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread1");
// 阻塞等待
object.wait();
System.out.println("thread1解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread1.start();
Thread thread2 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread2");
// 阻塞等待
object.wait();
System.out.println("thread2解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread2.start();
Thread thread3 = new Thread(() -> {
// 加锁 object
synchronized (object) {
System.out.println("thread3");
// 唤醒其他某个竞争 object资源 的线程
object.notify();
System.out.println("thread3唤醒某个竞争 object 资源的线程");
}
});
// 启动线程
thread3.start();
}
}
【执行结果】
【wait / notifyAll 情景】
public class Demo2 {
public volatile static int count = 3;
public static void main(String[] args) {
// 锁对象1
Object object = new Object();
// 锁对象2
Object newObject = new Object();
Thread thread1 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread1");
// 阻塞等待
object.wait();
System.out.println("thread1解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread1.start();
Thread thread2 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread2");
// 阻塞等待
object.wait();
System.out.println("thread2解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread2.start();
Thread thread3 = new Thread(() -> {
// 加锁 object
synchronized (object) {
System.out.println("thread3");
// 唤醒其他竞争 object资源 的线程
object.notifyAll();
System.out.println("thread3唤醒其他竞争 object 资源的线程");
}
});
// 启动线程
thread3.start();
}
}
【执行结果】
二,多个线程获取不相同锁资源后的wait/notify/notifyAll操作产生的结果。
【(synchronized(object))object.wait / (synchronized(newObject))newObject.notify / notifyAll情景】
public class Demo2 {
public volatile static int count = 3;
public static void main(String[] args) {
// 锁对象1
Object object = new Object();
// 锁对象2
Object newObject = new Object();
Thread thread1 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread1");
// 阻塞等待
object.wait();
System.out.println("thread1解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread1.start();
Thread thread2 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread2");
// 阻塞等待
object.wait();
System.out.println("thread2解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread2.start();
Thread thread3 = new Thread(() -> {
// 加锁 newObject
synchronized (newObject) {
System.out.println("thread3");
// 唤醒其他某个竞争 newObject资源 的线程
newObject.notify();
System.out.println("thread3唤醒某个竞争 newObject 资源的线程");
}
});
// 启动线程
thread3.start();
}
}
【执行结果】
三,多个线程获取相同/不同锁资源后却进行不同/相同wait/notify/notifyAll操作产生的结果。
【(synchronized(object))object.wait / (synchronized(object))newObject.notify/notifyAll 情景】
public class Demo2 {
public volatile static int count = 3;
public static void main(String[] args) {
// 锁对象1
Object object = new Object();
// 锁对象2
Object newObject = new Object();
Thread thread1 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread1");
// 阻塞等待
object.wait();
System.out.println("thread1解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread1.start();
Thread thread2 = new Thread(() -> {
try {
synchronized (object) {
System.out.println("thread2");
// 阻塞等待
object.wait();
System.out.println("thread2解放了");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
// 启动线程
thread2.start();
Thread thread3 = new Thread(() -> {
// 加锁 object
synchronized (object) {
System.out.println("thread3");
// 唤醒其他某个竞争 newObject资源 的线程
newObject.notify();
System.out.println("thread3唤醒某个竞争 newObject 资源的线程");
}
});
// 启动线程
thread3.start();
}
}
【执行结果】
在锁对象中调用不是该锁的对象的wait/notify/notifyAll操作时都会出现上面的 IllegalMonitorStateException 异常