• LeetCode多线程——按序打印:多种解法


    题目链接:https://leetcode-cn.com/problems/print-in-order/

    解法1:使用synchronizedwaitnotifyAll

    class Foo {
        private int state = 1;
    
        public Foo() {}
    
        public void first(Runnable printFirst) throws InterruptedException {
            synchronized(this) {
                while (state != 1) {
                    this.wait();
                }
                printFirst.run();
                state = 2;
                this.notifyAll();
            }
        }
    
        public void second(Runnable printSecond) throws InterruptedException {
            synchronized(this) {
                while (state != 2) {
                    this.wait();
                }
                printSecond.run();
                state = 3;
                this.notifyAll();
            }
        }
    
        public void third(Runnable printThird) throws InterruptedException {
            synchronized(this) {
                while (state != 3) {
                    this.wait();
                }
                printThird.run();
                state = 1;
                this.notifyAll();
            }
        }
    }
    

    解法2:使用ReentrantLockCondition

    class Foo {
        private int state = 1;
        private Lock lock = new ReentrantLock();
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();
    
        public Foo() {}
    
        public void first(Runnable printFirst) throws InterruptedException {
            lock.lock();
            try {
                while (state != 1) {
                    condition1.await();
                }
                printFirst.run();
                state = 2;
                condition2.signal();
            } finally {
                lock.unlock();
            }
        }
    
        public void second(Runnable printSecond) throws InterruptedException {
            lock.lock();
            try {
                while (state != 2) {
                    condition2.await();
                }
                printSecond.run();
                state = 3;
                condition3.signal();
            } finally {
                lock.unlock();
            }
        }
    
        public void third(Runnable printThird) throws InterruptedException {
            lock.lock();
            try {
                while (state != 3) {
                    condition3.await();
                }
                printThird.run();
                state = 1;
                condition1.signal();
            } finally {
                lock.unlock();
            }
        }
    }
    

    解法3:使用Semaphore

    class Foo {
        private Semaphore s1 = new Semaphore(0);
        private Semaphore s2 = new Semaphore(0);
    
        public Foo() {}
    
        public void first(Runnable printFirst) throws InterruptedException {
            printFirst.run();
            s1.release();
        }
    
        public void second(Runnable printSecond) throws InterruptedException { 
            s1.acquire();
            printSecond.run();
            s2.release();
        }
    
        public void third(Runnable printThird) throws InterruptedException {
            s2.acquire();
            printThird.run();
        }
    }
    

    解法4:使用CountDownLatch

    class Foo {
        private CountDownLatch c1 = new CountDownLatch(1);
        private CountDownLatch c2 = new CountDownLatch(1);
    
        public Foo() {}
    
        public void first(Runnable printFirst) throws InterruptedException {
            printFirst.run();
            c1.countDown();
        }
    
        public void second(Runnable printSecond) throws InterruptedException {
            c1.await();
            printSecond.run();
            c2.countDown();
        }
    
        public void third(Runnable printThird) throws InterruptedException {
            c2.await();
            printThird.run();
        }
    }
    

    解法5:使用CyclicBarrier

    class Foo {
        CyclicBarrier b1 = new CyclicBarrier(2);
        CyclicBarrier b2 = new CyclicBarrier(2);
    
        public Foo() {}
    
        public void first(Runnable printFirst) throws InterruptedException {
            try {
                printFirst.run();
                b1.await();
            } catch (Exception e) {}
        }
    
        public void second(Runnable printSecond) throws InterruptedException {
            try {
                b1.await();
                printSecond.run();
                b2.await();
            } catch (Exception e) {}
        }
    
        public void third(Runnable printThird) throws InterruptedException {
            try {
                b2.await();
                printThird.run();
            } catch (Exception e) {}
        }
    }
    

    解法6:使用BlockingQueue

    class Foo {
        BlockingQueue<Integer> q1 = new SynchronousQueue();
        BlockingQueue<Integer> q2 = new SynchronousQueue();
    
        public Foo() {}
    
        public void first(Runnable printFirst) throws InterruptedException {
            printFirst.run();
            q1.put(100);
        }
    
        public void second(Runnable printSecond) throws InterruptedException {
            
            q1.take();
            printSecond.run();
            q2.put(200);
        }
    
        public void third(Runnable printThird) throws InterruptedException {
            
            q2.take();
            printThird.run();
        }
    }
    

    解法7:自旋

    class Foo {
        private volatile boolean flag1 = false;
        private volatile boolean flag2 = false;
    
        public Foo() {}
    
        public void first(Runnable printFirst) throws InterruptedException {
            printFirst.run();
            flag1 = true;
        }
    
        public void second(Runnable printSecond) throws InterruptedException {
            while (!flag1);
            printSecond.run();
            flag2 = true;
        }
    
        public void third(Runnable printThird) throws InterruptedException {
            while (!flag2);
            printThird.run();
        }
    }
    
  • 相关阅读:
    sizeof--返回一个数据类型或变量占用的字节数
    跳出循环break continue
    c-for循环
    while循环语句
    什么是Python中的套接字编程?
    详解Vue八大生命周期钩子函数
    你应该知道的数仓安全
    云图说|华为云自研云数据库GaussDB NoSQL,兼容多款NoSQL接口的数据库服务
    又到一年“粽子节”,快来测测你包的粽子颜值几分
    云小课 | 华为云KYON之私网NAT网关
  • 原文地址:https://www.cnblogs.com/baiyuxuan/p/14529315.html
Copyright © 2020-2023  润新知