LeetCode1114.  按序打印[多线程] 6种解法

LeetCode1114. 按序打印[多线程] 6种解法

题目描述

image.png

话不多说 直接上代码,复习一下Lock、synchronized、还有CountDownLatch、Semaphore、CyclicBarrier等工具类

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.
        
    }
}