JUC-await和signal

本文详细阐述了Condition接口在Java多线程中的关键操作,强调了正确顺序(await先于signal)、锁的获取与释放以及常见异常情况的处理。通过实例展示了如何避免await和signal的错误调用,确保线程间的协调工作。
摘要由CSDN通过智能技术生成

正常情况

    public static void main(String[] args) {
        new Thread(()->{
            lock1.lock();
            try {
                System.out.println(Thread.currentThread().getName() + " come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + " 被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock1.unlock();
            }
        }, "t1").start();
        new Thread(()-> {
            lock1.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + " 发出唤醒通知");
            } finally {
                lock1.unlock();
            }
        }, "t2").start();
    }

t1 come in
t2 发出唤醒通知
t1 被唤醒 

异常情况:先signal,导致唤醒无法唤醒

    public static void main(String[] args) {
        new Thread(()->{
            try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
            lock1.lock();
            try {
                System.out.println(Thread.currentThread().getName() + " come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + " 被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock1.unlock();
            }
        }, "t1").start();
        new Thread(()-> {
            lock1.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + " 发出唤醒通知");
            } finally {
                lock1.unlock();
            }
        }, "t2").start();
    }

 

异常情况:await和signal没有在lock代码块中会报异常

    static Lock lock1 = new ReentrantLock();
    static Condition condition = lock1.newCondition();
    public static void main(String[] args) {
        new Thread(()->{
//            lock1.lock();
            try {
                System.out.println(Thread.currentThread().getName() + " come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + " 被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
//                lock1.unlock();
            }
        }, "t1").start();
        new Thread(()-> {
//            lock1.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + " 发出唤醒通知");
            } finally {
//                lock1.unlock();
            }
        }, "t2").start();
    }

总结:

Condtion中的线程等待和唤醒方法之前,需要先获取锁;

一定要先await(),后signal(),不要弄反了

进微信群和大佬面对面学习交流👇🏻👇🏻👇🏻

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值