LeetCode1114. 按序打印[多線程] 6種解法

做者:摺紙
我的博客: https://www.zhezhi.press
記錄菜雞的成長之旅!

題目描述

咱們提供了一個類:java

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}

三個不一樣的線程 A、B、C 將會共用一個Foo實例。異步

  • 一個將會調用first()方法
  • 一個將會調用second()方法
  • 還有一個將會調用third()方法
    請設計修改程序,以確保second()方法在first()方法以後被執行,third()方法在second()方法以後被執行。

示例 1:工具

輸入: [1,2,3]
輸出: "firstsecondthird"
解釋:
有三個線程會被異步啓動。
輸入 [1,2,3] 表示線程 A 將會調用 first() 方法,線程 B 將會調用 second() 方法,線程 C 將會調用 third() 方法。
正確的輸出是 "firstsecondthird"。

示例 2:測試

輸入: [1,3,2]
輸出: "firstsecondthird"
解釋:
輸入 [1,3,2] 表示線程 A 將會調用 first() 方法,線程 B 將會調用 third() 方法,線程 C 將會調用 second() 方法。
正確的輸出是 "firstsecondthird"。

提示ui

  • 儘管輸入中的數字彷佛暗示了順序,可是咱們並不保證線程在操做系統中的調度順序。
  • 你看到的輸入格式主要是爲了確保測試的全面性。

話很少說 直接上代碼,簡單複習一下Lock、synchronized、還有CountDownLatch、Semaphore、CyclicBarrier等工具類的使用this

ReentrantLock + Condition

class Foo {
        ReentrantLock lock = new ReentrantLock();
        Condition a = lock.newCondition();
        Condition b = lock.newCondition();
        Condition c = lock.newCondition();
        private volatile int status = 1;

        public Foo() {
        }

        public void first(Runnable printFirst) throws InterruptedException {
            

            try{
                lock.lock();
                while(status!=1){
                    a.await();
                }
                status = 2;
                printFirst.run();
                b.signal();
            }finally{
                lock.unlock();
            }
            // printFirst.run() outputs "first". Do not change or remove this line.

        }

        public void second(Runnable printSecond) throws InterruptedException {
            try{
                lock.lock();
                while(status!=2){
                    b.await();
                }
                status = 3;
                printSecond.run();
                c.signal();
            }finally{
                lock.unlock();
            }
            // printSecond.run() outputs "second". Do not change or remove this line.
        }

        public void third(Runnable printThird) throws InterruptedException {
            try{
                lock.lock();
                while(status!=3){
                    c.await();
                }
                status = 1;
                printThird.run();
                a.signal();
            }finally{
                lock.unlock();
            }
            // printThird.run() outputs "third". Do not change or remove this line.
            
        }
    }

LockSupport

class Foo {
    static Thread aThread = null,bThread = null,cThread = null;
    private volatile int status = 1;
    public Foo() {
        
    }

    public void first(Runnable printFirst) throws InterruptedException {
        aThread = Thread.currentThread();
        printFirst.run();
        status = 2;
        LockSupport.unpark(bThread);
        // printFirst.run() outputs "first". Do not change or remove this line.
        // printFirst.run();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        bThread = Thread.currentThread();
        while(status!=2)
            LockSupport.park();
        printSecond.run();
        status = 3;
        LockSupport.unpark(cThread);
        // printSecond.run() outputs "second". Do not change or remove this line.
        // printSecond.run();
    }

    public void third(Runnable printThird) throws InterruptedException {
        cThread = Thread.currentThread();
        while(status!=3)
            LockSupport.park();
        printThird.run();
        status = 1;
        LockSupport.unpark(aThread);
        // printThird.run() outputs "third". Do not change or remove this line.
        // printThird.run();
    }
}

synchronized + wait/notify

class Foo {
    Object lock = new Object();
    private volatile int status = 1;

    public Foo() {
        
    }

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

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

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

Semaphore

class Foo {
    Semaphore s = new Semaphore(1);
    private volatile int status = 1;

    public Foo() {
        
    }

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

    }

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

    public void third(Runnable printThird) throws InterruptedException {
        while(status!=3){
        }
        s.acquire();
        // printFirst.run() outputs "first". Do not change or remove this line.
        printThird.run();
        status = 1;
        s.release();
        
        // printThird.run() outputs "third". Do not change or remove this line.
    }
}

CountDownLatch

class Foo {
    CountDownLatch aLatch = new CountDownLatch(1);
    CountDownLatch bLatch = new CountDownLatch(1);


    public Foo() {
        
    }

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

    public void second(Runnable printSecond) throws InterruptedException {
        aLatch.await();//aLatch計數器爲1,當線程1打印完畢後--爲0,這裏再也不被阻塞 線程2開始打印
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        bLatch.countDown();
    }

    public void third(Runnable printThird) throws InterruptedException {
        bLatch.await();//bLatch計數器爲1,當線程1打印完畢後--爲0,這裏再也不被阻塞 線程2開始打印
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }
}

CyclicBarrier

class Foo {
    CyclicBarrier cy1 = new CyclicBarrier(2);
    CyclicBarrier cy2 = new CyclicBarrier(2);

    public Foo() {
        
    }

    public void first(Runnable printFirst) throws InterruptedException {
        
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        try{
            cy1.await();
        }catch(BrokenBarrierException e){
            e.printStackTrace();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {
        try{
            cy1.await(); //線程1已經經過了cy1表示線程1已經打印完畢
//當線程1 和2都到達此處後 線程2纔開始打印
            printSecond.run();
            cy2.await();//線程2到達屏障2
        }catch(BrokenBarrierException e){
            e.printStackTrace();
        }
        // printSecond.run() outputs "second". Do not change or remove this line.
    }

    public void third(Runnable printThird) throws InterruptedException {
        try{
            cy2.await();//線程2打印完畢後線程3纔開始打印
            printThird.run();
        }catch(BrokenBarrierException e){
            e.printStackTrace();
        }
        // printThird.run() outputs "third". Do not change or remove this line.
        
    }
}
相關文章
相關標籤/搜索