LeetCode 1114. 按序打印

本文介绍了Java中实现三个线程按特定顺序执行的四种方法:使用同步原语、原子操作类、CountDownLatch和Semaphore。这些方法都是为了解决多线程环境下线程间的协作问题,确保打印顺序的正确性。

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

1114. 按序打印 - 力扣(LeetCode) (leetcode-cn.com)

方法一:同步原语


    private volatile int flag = 1;
    private final Object object = new Object();

    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        synchronized (object) {
            while(flag!=1){
                object.wait();
            }
            printFirst.run();
            flag = 2;
            object.notifyAll();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (object) {
            while(flag != 2) {
                object.wait();
            }
            printSecond.run();
            flag = 3;
            object.notifyAll();
        }

    }

    public void third(Runnable printThird) throws InterruptedException {
        synchronized (object) {
            while (flag != 3) {
                object.wait();
            }
            printThird.run();
        }

    }

方式二:原子操作类


import java.util.concurrent.atomic.AtomicInteger;

class Foo {

    private final AtomicInteger flag = new AtomicInteger(0);

    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
       printFirst.run();
       flag.incrementAndGet();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while (flag.get() != 1) {

        }
        printSecond.run();
        flag.incrementAndGet();
    }

    public void third(Runnable printThird) throws InterruptedException {
        while (flag.get() != 2) {

        }
        printThird.run();
    }
}

方式三:CountDownLatch


class Foo {

    private final CountDownLatch first;
    private final CountDownLatch second;

    public Foo() {
        first = new CountDownLatch(1);
        second = new CountDownLatch(1);
    }

    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        first.countDown();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        first.await();
        printSecond.run();
        second.countDown();
    }

    public void third(Runnable printThird) throws InterruptedException {
        second.await();
        printThird.run();
    }
}

方法四:Semaphore

private Semaphore semaphore = new Semaphore(0);
    
    public Foo() {
    }

    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        semaphore.release();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while(semaphore.availablePermits() != 1) {
            
        }
        printSecond.run();
        semaphore.release();
    }

    public void third(Runnable printThird) throws InterruptedException {
        while(semaphore.availablePermits()!=2) {
            
        }
        printThird.run();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZuckD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值