当前位置: 首页>编程语言>正文

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();
    }
}

https://www.xamrdz.com/lan/5g31994382.html

相关文章: