Condition等待唤醒

前言

在Java中,每个对象都可以调用 Object 的 wait/notify 方法来实现等待/通知机制。而 Condition 接口也提供了类似的方法,也能实现等待唤醒,Condition需要在ReentrantLock下使用,也就是需要上锁和释放锁,否则也会报java.lang.IllegalMonitorStateException

Condition使用

Condition方法

Condition提供await()、signal()、signalAll()方法,其中,await()是让线程等待,signal()是唤醒一个线程,signalAll()是唤醒全部线程,当前线程调用condition.await()方法后,会释放 lock 然后加入到等待队列,直到被 signal/signalAll 方法唤醒

Condition等待唤醒使用

1、await()等待唤醒

ini 复制代码
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {

    public static void main(String[] args) {
        final ReentrantLock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();

        Thread thread1 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                System.out.println(name + " <==进入条件队列等待");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
            System.out.println(name + ":线程被唤醒");
        }, "aa");

        thread1.start();

        Thread thread2 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                Thread.sleep(5000);
                System.out.println(name + ":开始唤醒线程");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            condition.signalAll();
            lock.unlock();
            System.out.println(name + ":唤醒结束");
        }, "通知线程");

        thread2.start();
    }
}

输出结果为

signal()只能唤醒一个线程

ini 复制代码
package cn.com.ut.july.wait;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {

    public static void main(String[] args) {
        final ReentrantLock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();

        Thread thread1 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                System.out.println(name + " <==进入条件队列等待");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
            System.out.println(name + ":线程被唤醒");
        }, "aa");

        thread1.start();

        Thread thread3 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                System.out.println(name + " <==进入条件队列等待");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
            System.out.println(name + ":线程被唤醒");
        }, "aa1");

        thread3.start();

        Thread thread2 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                Thread.sleep(5000);
                System.out.println(name + ":开始唤醒线程");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            condition.signal();
            lock.unlock();
            System.out.println(name + ":唤醒结束");
        }, "通知线程");

        thread2.start();
    }
}

输出结果为

signalAll()唤醒全部线程

ini 复制代码
package cn.com.ut.july.wait;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {

    public static void main(String[] args) {
        final ReentrantLock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();

        Thread thread1 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                System.out.println(name + " <==进入条件队列等待");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
            System.out.println(name + ":线程被唤醒");
        }, "aa");

        thread1.start();

        Thread thread3 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                System.out.println(name + " <==进入条件队列等待");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
            System.out.println(name + ":线程被唤醒");
        }, "aa1");

        thread3.start();

        Thread thread2 = new Thread(() -> {
            String name = Thread.currentThread().getName();

            lock.lock();
            try {
                Thread.sleep(5000);
                System.out.println(name + ":开始唤醒线程");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            condition.signalAll();
            lock.unlock();
            System.out.println(name + ":唤醒结束");
        }, "通知线程");

        thread2.start();
    }
}

输出结果为

Condition实现三个线程顺序输出

scss 复制代码
package cn.com.ut.july.wait;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionDemo {

    public static void main(String[] args) throws InterruptedException {

        ReentrantLock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();

        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(500);
                    System.out.println("A");
                    lock.lock();
                    condition2.signal();
                    condition1.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();
        Thread.sleep(100);
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(500);
                    System.out.println("B");
                    lock.lock();
                    condition3.signal();
                    condition2.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();
        Thread.sleep(100);
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(500);
                    System.out.println("C");
                    lock.lock();
                    condition1.signal();
                    condition3.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();

    }

}

输出结果为

使用Thread.sleep()是为了保证先start()的线程先启动,以及不输出那么快

总结

Condition的await()和signal()基于Lock,相比于基于Object的wait()和notify()方法,它提供更加灵活的等待通知的机制,并且是基于AQS队列形式,但是在开发过程中,根据自己喜好选择

相关推荐
Seven9713 分钟前
【Guava】IO工具
java
Goboy16 分钟前
从崩溃到升职:腾讯云EdgeOne Pages MCP拯救了我的996危机
后端·程序员·架构
独行soc18 分钟前
2025年渗透测试面试题总结-某腾某讯-技术安全实习生升级(题目+回答)
java·python·安全·web安全·面试·职场和发展·红蓝攻防
用键盘当武器的秋刀鱼37 分钟前
springBoot统一响应类型3.5.3版本
java·spring boot·spring
qq_4315101643 分钟前
tomcat组件概览
java·tomcat
栗筝i1 小时前
Spring 核心技术解析【纯干货版】- XVII:Spring 网络模块 Spring-WebFlux 模块精讲
java·网络·spring
工业互联网专业1 小时前
基于springcloud微服务架构的巡游出租管理平台
java·vue.js·spring cloud·微服务·毕业设计·源码·课程设计
cwtlw1 小时前
Spring相关面试题总结
java·笔记·后端·spring
漫天转悠1 小时前
Java8 到 Java21 系列之 Lambda 表达式:函数式编程的开端(Java 8)
java·开发语言
yiridancan1 小时前
深入浅出:Spring Bean 的初始化流程详解
java·开发语言·后端·spring