三个线程按顺序交替打印 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();
    }
相关推荐
Dong雨16 分钟前
Spring Boot 事务详解
java·spring boot·后端
小吕学编程16 分钟前
破局 MySQL 死锁:深入理解锁机制与高效解决方案
java·数据库·sql·mysql
Java版蜡笔小新17 分钟前
数字与静态
java·开发语言·学习
爱的叹息44 分钟前
Feture常见实现类(FutureTask、CompletableFuture、ListenableFuture)对比
java·开发语言·python
飞跑的鱼1 小时前
Java面试八股—Redis篇
java·redis·面试
不爱学英文的码字机器1 小时前
[操作系统] 进程间通信:匿名管道原理与操作
java·服务器·数据库
Lecea_L2 小时前
深入了解Java中的多线程:实现方法与常见坑
java
whysqwhw2 小时前
Spring AOP:简化动态代理使用,支持声明式切面。
java
Lill_bin2 小时前
冒泡排序:古老算法中的智慧启示
java·开发语言·数据结构·分布式·算法
Vacant Seat2 小时前
二叉树-路径总和III
java·数据结构·算法·二叉树