欢迎访问宙启技术站
智能推送

怎么用CountDownLatch完成LeetCode1114

发布时间:2023-05-16 13:07:37

LeetCode1114是一道经典的线程同步问题,要求按顺序执行三个线程。在Java中,可以使用CountDownLatch来实现线程同步。这篇文章将介绍如何使用CountDownLatch完成LeetCode1114。

问题描述:

使用三个不同的线程按顺序打印“first”、“second”和“third”,实现以下函数:

public class Foo {

public void first(Runnable printFirst) throws InterruptedException {

  // printFirst.run() outputs "first". Do not change or remove this line.

  printFirst.run();

}

public void second(Runnable printSecond) throws InterruptedException {

  // printSecond.run() outputs "second". Do not change or remove this line.

  printSecond.run();

}

public void third(Runnable printThird) throws InterruptedException {

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

  printThird.run();

}

}

解题思路:

要按顺序执行三个线程,我们可以使用CountDownLatch。CountDownLatch是一个同步工具,可以让一个或多个线程等待一些初始化操作完成后再继续执行。在本问题中,我们使用三个CountDownLatch来实现线程同步,每个CountDownLatch用于控制一个线程的执行。

首先我们需要创建三个CountDownLatch。我们将 个CountDownLatch命名为“secondLatch”,第二个CountDownLatch命名为“thirdLatch”,第三个CountDownLatch命名为“finishLatch”。我们将它们的初始值都设置为1。

其次,我们需要创建三个线程,分别执行first、second、third方法。在first方法中,我们什么都不需要做,直接执行printFirst.run()。在second方法中,我们需要等待first方法执行完成后再执行printSecond.run()。我们可以使用secondLatch来实现这个等待。在third方法中,我们需要等待second方法和first方法都执行完成后再执行printThird.run()。我们可以使用thirdLatch和finishLatch来实现这个等待。

最后,我们需要在main方法中启动这三个线程,并等待它们执行完成。我们可以使用finishLatch来实现等待。

代码实现:

public class Foo {

    private CountDownLatch secondLatch;

    private CountDownLatch thirdLatch;

    private CountDownLatch finishLatch;

    public Foo() {

        secondLatch = new CountDownLatch(1);

        thirdLatch = new CountDownLatch(1);

        finishLatch = new CountDownLatch(3);

    }

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.

        printFirst.run();

        secondLatch.countDown();

    }

    public void second(Runnable printSecond) throws InterruptedException {

        // printSecond.run() outputs "second". Do not change or remove this line.

        secondLatch.await();

        printSecond.run();

        thirdLatch.countDown();

    }

    public void third(Runnable printThird) throws InterruptedException {

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

        thirdLatch.await();

        printThird.run();

        finishLatch.countDown();

    }

    public static void main(String[] args) throws InterruptedException {

        Foo foo = new Foo();

        Thread firstThread = new Thread(() -> {

            try {

                foo.first(() -> System.out.print("first"));

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        });

        Thread secondThread = new Thread(() -> {

            try {

                foo.second(() -> System.out.print("second"));

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        });

        Thread thirdThread = new Thread(() -> {

            try {

                foo.third(() -> System.out.print("third"));

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        });

        firstThread.start();

        secondThread.start();

        thirdThread.start();

        foo.finishLatch.await(); // 等待三个线程执行完成

    }

}

总结:

使用CountDownLatch可以很方便地实现线程同步。在本问题中,我们使用三个CountDownLatch来控制三个线程的执行顺序。通过对CountDownLatch的使用,我们可以将问题简单地解决。