SoFunction
Updated on 2025-05-16

An example of Java multithreaded concurrent JUC package CountDownLatch locking

An example of Java multithreaded concurrent JUC package CountDownLatch locking

Updated: May 16, 2025 10:42:16 Author: Erliuba
This article mainly introduces the example of Java multithreaded concurrent JUC package CountDownLatch locking, which has good reference value. I hope it will be helpful to everyone. If there are any errors or no complete considerations, I hope you will be very encouraged.

Java multithreaded concurrent JUC package CountDownLatch lock

Locking is a disposable object

Once it enters the termination state, it cannot be reset.

public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        int count = 5;
        CountDownLatch countDownLatch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            new Tangerine((i),countDownLatch).start();
        }
        ("main thread waiting\n");
        ();
        ("Main thread ends running");
    }

    static class Tangerine extends Thread {
        final CountDownLatch countDownLatch;
        public Tangerine(String name,CountDownLatch countDownLatch) {
            super(name);
             = countDownLatch;
        }

        @Override
        public void run() {
            ("Thread %s is running\n", ());
            ();
        }
    }
}
  • await()Wait until the value in the counter decreases to 0.
  • await(long timeout, TimeUnit unit)You can set the timeout time yourself. Once this time exceeds, the await thread will be awakened. If true returns, it means that the counter is 0, otherwise, it will not be 0.
  • countDown()Decrement the counter value by 1.
  • getCount()Get the value of the current counter.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.

  • Java
  • Multi-threaded concurrency
  • JUC package
  • CountDownLatch
  • atresia

Related Articles

  • The correct solution to the exception (it is effective for personal testing!)

    This article mainly introduces the correct solution to exceptions. The method introduced in the article is effective and is an exception class in the Java programming language. It means that the specified character set is not supported. Friends who need it can refer to it.
    2024-02-02
  • Java: Error resolution

    This article mainly introduces Java: error resolution, mainly pointing out that when the client module-sso tries to access the service through the load balancer, the load balancer does not find an available server to handle the request. Let's introduce the solution below
    2024-08-08
  • How to implement the example code based on opencv panoramic diagram synthesis

    I believe everyone is familiar with panoramic images. The following article mainly introduces relevant materials on how Java can implement panoramic images synthesis based on opencv. The article introduces the example code in detail. Friends who need it can refer to it. Let’s learn and learn together with the editor.
    2018-07-07
  • Summary of the Bean to Map problem in Java

    There are many pitfalls in Java Bean to Map. The most common ones are type loss and attribute name resolution errors. The following article mainly introduces relevant information about the summary of the Bean to Map problem in Java. The article introduces it in detail through the example code. If you need it, you can refer to it.
    2023-06-06
  • Detailed explanation of the installation, configuration and use of eclipse's git plug-in

    This article mainly introduces the detailed explanation of the installation, configuration and use of eclipse's git plug-in. The example code is introduced in this article in detail, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.
    2020-07-07
  • Detailed explanation of the use examples of new JDK19 features

    This article mainly introduces detailed explanations of the use of new features of JDK19. Friends in need can refer to it for reference. I hope it can be helpful. I wish you more progress and get promoted as soon as possible to get a salary increase.
    2022-09-09
  • Mybatis dynamic sql super detailed explanation

    Dynamic SQL is one of the powerful features of MyBatis. As the name suggests, it is dynamic SQL, which is able to flexibly splice complete SQL statements based on certain conditions. The following article mainly introduces relevant information about MyBatis dynamic SQL. Friends who need it can refer to it.
    2023-04-04
  • A deep understanding of Java reflection mechanism

    This article mainly introduces relevant information for in-depth understanding of Java reflection mechanism. I hope that you can understand this part of the content through this article. Friends who need it can refer to it.
    2017-09-09
  • Detailed implementation method for exporting pdf files in java

    This article mainly introduces the detailed implementation method of Java exporting pdf files, including creating templates, obtaining Chinese font files, implementing back-end services, and making requests on the front-end and generating download links. Friends who need it can refer to it
    2025-03-03
  • How to customize 404 pages in springboot project

    Today, when clicking on the menu, I accidentally clicked on a non-existent page, and then saw a default 404 page given by the browser. This article mainly introduces how to customize 404 pages in the springboot project. Friends who need it can refer to it.
    2024-05-05

Latest Comments