java 三个线程依次输出abc

本文通过两个示例介绍如何使用Java中的锁和条件变量及阻塞队列来控制多个线程按特定顺序执行任务。首先展示了一个利用ReentrantLock和Condition实现的线程间通信方案,确保线程A、B、C按顺序各执行15次;接着提供了一种基于阻塞队列的方法,同样实现了相同的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

启动三个线程,线程1打印a,线程2打印b,线程3打印c,依次打印15次。

Lock Condition实现

代码如下:
public class NAbcPrinter {
    private final Lock lock = new ReentrantLock();
    private final Condition con1 = lock.newCondition();
    private final Condition con2 = lock.newCondition();
    private final Condition con3 = lock.newCondition();
    private int no = 1;

    public void process1() {
        lock.lock();
        try {
            while (no != 1) con1.await();
            System.out.println(Thread.currentThread().getName() + " a");
            no = 2;
            con2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void process2() {
        lock.lock();
        try {
            while (no != 2) con2.await();
            System.out.println(Thread.currentThread().getName() + " b");
            no = 3;
            con3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void process3() {
        lock.lock();
        try {
            while (no != 3) con3.await();
            System.out.println(Thread.currentThread().getName() + " c");
            no = 1;
            con1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        NAbcPrinter p = new NAbcPrinter();
        new Thread(() -> {
            for (int i = 0; i < 15; i++) p.process1();
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 15; i++)
                p.process2();
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 15; i++)
                p.process3();
        }).start();
    }
}

阻塞队列实现:

public class AbcPrinter {
    private final BlockingQueue<Integer> bq1 = new ArrayBlockingQueue<>(1);
    private final BlockingQueue<Integer> bq2 = new ArrayBlockingQueue<>(1);
    private final BlockingQueue<Integer> bq3 = new ArrayBlockingQueue<>(1);

    {
        try {
            bq1.put(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void process1() {
        try {
            bq1.take();
            System.out.println(Thread.currentThread().getName() + " a");
            bq2.put(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void process2() {
        try {
            bq2.take();
            System.out.println(Thread.currentThread().getName() + " b");
            bq3.put(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void process3() {
        try {
            bq3.take();
            System.out.println(Thread.currentThread().getName() + " c");
            bq1.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        NAbcPrinter p = new NAbcPrinter();
        new Thread(() -> {
            for (int i = 0; i < 15; i++) p.process1();
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 15; i++)
                p.process2();
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 15; i++)
                p.process3();
        }).start();
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值