没有条件创造条件Condition

本文详细对比了Java中Object的wait()和notify()方法与Lock接口的Condition对象的wait()和signal()方法的异同。通过示例代码展示了它们在多线程同步中的应用,强调了wait()会释放锁而signal()不会立即释放锁这一关键区别。

作者: 西魏陶渊明 博客: https://blog.springlearn.cn/ (opens new window)

西魏陶渊明

莫笑少年江湖梦,谁不少年梦江湖

https://blog.youkuaiyun.com/a1439775520/article/details/98471610

Condition 是为了调换 Object 中的 wait() 和notify().

APIObjectCondition
等待wait()wait()
通知notify()signal()
通知所有notifyAll()signalAll()

wait是会释放锁

# 相同点

  • 都必须被包裹在同步代码块中,即加锁
  • 当调用wait都会释放锁

# 不同点

  • Object 依赖 synchronized 锁
  • Condition 依赖 Lock 锁

# ObjectWait

运行结果

  • 当前线程进入线程一
  • 当前线程进入线程二
  • 当前线程释放main
  • 当前线程释放main
  • 当前线程退出线程一
  • 当前线程退出线程二
/**
 * @author liuxin
 * 2022/1/5 12:24 上午
 */
public class ObjectWait {

    public synchronized void objWait() throws Exception {
        System.out.println("当前线程进入" + Thread.currentThread().getName());
        wait();
        System.out.println("当前线程退出" + Thread.currentThread().getName());
    }

    public synchronized void objNotify() throws Exception {
        System.out.println("当前线程释放" + Thread.currentThread().getName());
        notify();
    }

    public static void main(String[] args) throws Exception {
        ObjectWait objectWait = new ObjectWait();
        // 使用synchronized修饰方法,就是锁的是当前这个示例synchronized(this)
        new Thread(() -> {
            try {
                // 当前进入等待,然后释放锁。wait会释放锁
                objectWait.objWait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"线程一").start();

        // 使用synchronized修饰方法,就是锁的是当前这个示例synchronized(this)
        new Thread(() -> {
            try {
                // 当前进入等待,然后释放锁。wait会释放锁
                objectWait.objWait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"线程二").start();
        Thread.sleep(1000L);
        // 释放锁后才会放行
        objectWait.objNotify();
        // 释放锁后才会放行
        objectWait.objNotify();
    }
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

# LockWait

运行结果

  • 当前线程进入线程一
  • 当前线程进入线程二
  • 当前线程释放main
  • 当前线程释放main
  • 当前线程退出线程一
  • 当前线程退出线程二
/**
 * @author liuxin
 * 2022/1/5 24:24 上午
 */
public class LockWait {

    private ReentrantLock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    public void lockWait() throws Exception {
        lock.lock();
        System.out.println("当前线程进入" + Thread.currentThread().getName());
        condition.await();
        System.out.println("当前线程退出" + Thread.currentThread().getName());
        lock.unlock();
    }

    public void lockNotify() {
        lock.lock();
        System.out.println("当前线程释放" + Thread.currentThread().getName());
        condition.signal();
        lock.unlock();
    }

    public static void main(String[] args) throws Exception {
        LockWait lockWait = new LockWait();
        // lock.lock()  线程一: 获取锁,然后wait之后,进入释放锁,然后进入到等待队列
        new Thread(() -> {
            try {
                // 当前进入等待,然后释放锁。
                lockWait.lockWait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "线程一").start();
        // lock.lock()  当前线程二获取锁,然后wait之后,进入释放锁,然后进入到等待队列
        new Thread(() -> {
            try {
                // 当前进入等待,然后释放锁。
                lockWait.lockWait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"线程二").start();

        Thread.sleep(1000L);
        // 调用第一次,会唤醒线程一,继续向下执行
        lockWait.lockNotify();
        // 调用第二次,会唤醒线程二,继续向下执行
        lockWait.lockNotify();
    }
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西魏陶渊明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值