多线程按顺序打印

这篇博客介绍了如何在多线程环境下确保特定执行顺序,提供了四种方法:ReentrantLock、volatile变量、wait-notify和信号量。通过示例代码展示了如何使用这些机制来保证first、second和third方法的顺序执行。
摘要由CSDN通过智能技术生成

多线程按顺序打印


来源于leetcode1114
我们提供了一个类:
public class Foo {
public void first() { print(“first”); }
public void second() { print(“second”); }
public void third() { print(“third”); }
}
三个不同的线程将会共用一个 Foo 实例。

  • 线程 A 将会调用 first() 方法
  • 线程 B 将会调用 second() 方法
  • 线程 C 将会调用 third() 方法

请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。

本文提供四种方法ReentrantLock,wait-notify,semaphore,cas自旋。其他方法还可以用CountdownLatch,ArrayBlockingQueue。

方法一

//第一种方法用ReentrantLock
class Foo {
    private volatile int flag = 1;
    private ReentrantLock reentrantLock = new ReentrantLock();
    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        for (;;) {
            if (flag == 1) {
                reentrantLock.lock();
                break;
            }
        }
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        flag = 2;
        reentrantLock.unlock();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        for (;;) {
            if (flag == 2) {
                reentrantLock.lock();
                break;
            }
        }
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        flag = 3;
        reentrantLock.unlock();
    }

    public void third(Runnable printThird) throws InterruptedException {
        for (;;) {
            if (flag == 3) {
                reentrantLock.lock();
                break;
            }
        }
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
        reentrantLock.unlock();
    }
}

方法二

//既然都volatile的flag了,直接下面这么写
class Foo {
    private volatile int flag = 1;
    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        while (flag != 1) {
            
        }
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        flag = 2;
        
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while (flag != 2) {
            
        }
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        flag = 3;
    }

    public void third(Runnable printThird) throws InterruptedException {
        while (flag != 3) {
            
        }
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }
}

方法三

//wait-notify
class Foo {
    private Object object = new Object();
    private volatile int flag = 1;
    public Foo() {

    }

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

            // printFirst.run() outputs "first". Do not change or remove this line.
            printFirst.run();
            flag = 2;
            object.notifyAll();
        }
    }

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

            // printSecond.run() outputs "second". Do not change or remove this line.
            printSecond.run();
            flag = 3;
            object.notifyAll();
        }
    }

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

            // printThird.run() outputs "third". Do not change or remove this line.
            printThird.run();
            object.notifyAll();
        }
    }
}

方法四

//信号量
class Foo {
    private Semaphore semaphore1 = new Semaphore(0);
    private Semaphore semaphore2 = new Semaphore(0);
    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        semaphore1.release();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        semaphore1.acquire();
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        semaphore2.release();
    }

    public void third(Runnable printThird) throws InterruptedException {
        semaphore2.acquire();
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
        semaphore2.release();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值