java - 多线程使用synchronized给相同/或者不同对象进行加锁后进行wait/notify/notifyAll操作产生的结果

一,多个线程获取相同锁资源后的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 异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值