三个线程轮流执行按顺序打印123

本文介绍了如何使用Java的ReentrantLock和Condition接口来实现三个线程按照顺序123无限循环打印,确保每个线程在完成当前任务后等待下一个线程唤醒。
摘要由CSDN通过智能技术生成

三个线程无限循环轮流执行按顺序打印123

实现思路:
三个线程基于不同的Condition完成等待和继续

最简单的实现(不考虑任何封装):

public class Solution {

    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(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("1");
                    try {
                        lock.lock();
                        condition2.signal();
                        condition1.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }).start();
        //线程start之后不一定立即run起来-这里测试用的代码-用来先后启动目标线程
        Thread.sleep(100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("2");
                    try {
                        lock.lock();
                        condition3.signal();
                        condition2.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }).start();
        //线程start之后不一定立即run起来-这里测试用的代码-用来先后启动目标线程
        Thread.sleep(100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("3");
                    try {
                        lock.lock();
                        condition1.signal();
                        condition3.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }).start();

    }

}

简单封装一下

public class Solution {

    private static class PrintRunnable implements Runnable {

        private final String value;

        private final Lock lock;
        private final Condition cur, next;

        public PrintRunnable(String value, Lock lock, Condition cur, Condition next) {
            this.value = value;
            this.lock = lock;
            this.cur = cur;
            this.next = next;
        }

        @Override
        public void run() {
            while (true) {
                System.out.println(value);
                try {
                    lock.lock();
                    //这里
                    next.signal();
                    //这里特别注意:await会阻塞线程-因此要在这之前触发signal
                    cur.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }

    }

    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(new PrintRunnable("1", lock, condition1, condition2)).start();
        Thread.sleep(100);
        new Thread(new PrintRunnable("2", lock, condition2, condition3)).start();
        Thread.sleep(100);
        new Thread(new PrintRunnable("3", lock, condition3, condition1)).start();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值