三个线程按顺序交替打印 A B C

方法一:ReentrantLock+Condition

java 复制代码
public static void method1() {
        ReentrantLock lock = new ReentrantLock();
        Condition condA = lock.newCondition();
        Condition condB = lock.newCondition();
        Condition condC = lock.newCondition();

        new Thread(() -> {
            while (true) {
                lock.lock();
                try {
                    condB.signal();
                    System.out.println("A");
                    Thread.sleep(1000);
                    condA.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                lock.lock();
                try {
                    condC.signal();
                    System.out.println("B");
                    Thread.sleep(1000);
                    condB.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                lock.lock();
                try {
                    condA.signal();
                    System.out.println("C");
                    Thread.sleep(1000);
                    condC.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }

方法二:synchronized

java 复制代码
public static void method2() {

        Object lock = new Object();
        new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    while (state != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("A");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    state = 2;
                    lock.notifyAll();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    while (state != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("B");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    state = 3;
                    lock.notifyAll();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    while (state != 3) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    state = 1;
                    lock.notifyAll();
                }
            }
        }).start();
    }

方法三:Semaphore

java 复制代码
public static void method3() {
        Semaphore semaphoreA = new Semaphore(1);
        Semaphore semaphoreB = new Semaphore(0);
        Semaphore semaphoreC = new Semaphore(0);

        new Thread(() -> {
            while (true){
                try {
                    semaphoreA.acquire();
                    System.out.println("A");
                    Thread.sleep(1000);
                    semaphoreB.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(() -> {
            while (true){
                try {
                    semaphoreB.acquire();
                    System.out.println("B");
                    Thread.sleep(1000);
                    semaphoreC.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(() -> {
            while (true){
                try {
                    semaphoreC.acquire();
                    System.out.println("C");
                    Thread.sleep(1000);
                    semaphoreA.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
相关推荐
重生之后端学习3 分钟前
day23-集合(泛型&Set&数据结构)
java·开发语言·数据结构·算法
码农飞哥36 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务的技术问答与解析
java·数据库·spring boot·安全·微服务·面试·电商
雨落白笙36 分钟前
端口转发与跨域处理
java
曼岛_1 小时前
[Java实战]Spring Boot 定时任务(十五)
java·spring boot·python
oliveira-time1 小时前
app加固
java
菲兹园长1 小时前
MyBatis-Plus
java·开发语言·mybatis
计算机学姐1 小时前
基于SpringBoot的在线教育管理系统
java·vue.js·spring boot·后端·mysql·spring·mybatis
菜鸟破茧计划1 小时前
滑动窗口:穿越数据的时光机
java·数据结构·算法
windwant2 小时前
深入解析Http11AprProtocol:Tomcat高性能通信的底层原理
java·tomcat
Minyy112 小时前
“爱生活”小项目问题总结
java·数据库·spring boot·spring·maven·intellij-idea