GithubHelp home page GithubHelp logo

CountDownLatchDemo about java-concurrency HOT 1 OPEN

cl0610 avatar cl0610 commented on May 10, 2024
CountDownLatchDemo

from java-concurrency.

Comments (1)

Buywatermelon avatar Buywatermelon commented on May 10, 2024

发现这已经是一个2019年的Issue了,想必提问者随着阅历的增加早已找到了答案。而我作为一个正在学习的萌新在2021年看到了这个问题,便尝试着来解答一下。希望对未来的萌新有所帮助

  • 我写了一段至少打印顺序正确的demo
    public void testCountDownLatch() throws InterruptedException {
        CountDownLatch judgeLatch = new CountDownLatch(1);
        CountDownLatch sportLatch = new CountDownLatch(8);
        CountDownLatch sportWaitLatch = new CountDownLatch(8);

        ExecutorService executorService = Executors.newFixedThreadPool(8);
        for (int i = 0; i < 8; i++) {
            executorService.execute(() -> {
                try {
                    log.info(Thread.currentThread().getName() + "运动员等待裁判员响哨!!!");
                    sportWaitLatch.countDown();

                    judgeLatch.await();
                    log.info(Thread.currentThread().getName() + "正在全力冲刺!!!");
                    log.info(Thread.currentThread().getName() + "到达终点");
                    sportLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        sportWaitLatch.await();
        log.info("裁判员发号施令啦!!!");
        judgeLatch.countDown();

        sportLatch.await();
        log.info("所有运动员到达终点,比赛结束");
    }
  • 可以看出区别在于打印“裁判员发号施令啦!!!”语句时,我用一个新的CountDownLatch对象await住了其他线程。
  • 那么很容易就知道原本代码的问题在于:主线程在执行打印“裁判员发号施令啦!!!”时,线程池的遍历执行还没有集中执行到await()处。

from java-concurrency.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.