(The previous “programmer 7 cat” account of the Nuggets could not be found, so I had to open a new one, and the first 2 articles will be moved here)


The comic adaptation of hong of the one of the concurrent Java project: atresia CountDownLatch family eat dinner together “https://blog.csdn.net/lmj623565791/article/details/26626391

Thank you for your support!

One day, the Kitty family decided to go to a restaurant for dinner, and it was almost time to leave work when the family· F3 group started chatting

Here’s a joke from the poor cat father

It’s time to get off work (cheers!) Everyone started to go to the restaurant

Public class Test1 {/** * public static void */fatherToRes()
    {
        System.out.println("It takes dad three hours to walk to the hotel."); } /** * public static voidmotherToRes()
    {
        System.out.println(It takes mom two hours to get to the hotel by bus.); } /** * public static void */meToRes()
    {
        System.out.println("It takes me an hour to get to the hotel by subway."); } /** * public static void */togetherToEat()
    {
        System.out.println("The whole family is here, and we're eating."); } public static void main(String[] args) { fatherToRes(); motherToRes(); meToRes(); togetherToEat(); }}Copy the code

The output

It takes dad three hours to walk to the hotel. It takes mom two hours to get to the hotel by bus. It takes me an hour to go to the hotel by subway. The whole family arrived and began to eatCopy the code

! Stop! Is there something wrong, why is the cat father to walk for 3 hours, the mother to take a bus for 2 hours, and then wait until the cat to take the subway for 1 hour, this whole 6 hours, then what to eat, just buy a midnight snack!

The father, mother and kitten all set off at their own time, so we change the scene

Open the three threads of dad walking, mom taking the bus and kitten taking the subway, as follows

public static void main(String[] args)
    {
        new Thread()
        {
            public void run()
            {
                fatherToRes();
            };
        }.start();
        new Thread()
        {
            public void run()
            {
                motherToRes();
            };
        }.start();
        new Thread()
        {
            public void run()
            {
                meToRes();
            };
        }.start();
        togetherToEat();
    }
Copy the code

The output

It takes me an hour to get to the hotel by subway. It takes mom two hours to get to the hotel by bus. It takes dad three hours to walk to the hotel.Copy the code

Meow? Meow? Meow? !

Open the thread concurrency, but it seems also wrong ah, a three are not to the restaurant, how can eat dinner first?

So let’s do it another way

private static volatile int i = 3;
    public static void main(String[] args)
    {
        new Thread()
        {
            public void run()
            {
                fatherToRes();
                i--;
            };
        }.start();
        new Thread()
        {
            public void run()
            {
                motherToRes();
                i--;
            };
        }.start();
        new Thread()
        {
            public void run()
            {
                meToRes();
                i--;
            };
        }.start();
        while(i ! = 0); togetherToEat(); }Copy the code

The key modifier volatile

Volatile: When multiple threads operate on the same variable, it ensures that changes to the variable are visible to other threads. But volatile does not guarantee atomicity, and I — is not an atomic operation. So suggest normal use synchronized blocks or AtomicLong. DecrementAndGet implementation – ().

Come back to the ~

private static CountDownLatch latch = new CountDownLatch(3);
    public static void main(String[] args) throws InterruptedException
    {
        new Thread()
        {
            public void run()
            {
                fatherToRes();
                latch.countDown();
            };
        }.start();
        new Thread()
        {
            public void run()
            {
                motherToRes();
                latch.countDown();
            };
        }.start();
        new Thread()
        {
            public void run()
            {
                meToRes();
                latch.countDown();
            };
        }.start();
        latch.await();
        togetherToEat();
    }
Copy the code

The output

It takes me an hour to go to the hotel by subway. It takes mom two hours to get to the hotel by bus. It takes dad three hours to walk to the hotel. The whole family arrived and began to eatCopy the code

CountDowmLatch is a flexible locking implementation that contains a counter that is initialized to a positive number indicating the number of events to wait for. The countDown method decrement the counter to indicate that an event has occurred, and the await method waits for the counter to reach 0 to indicate that everything that needs to wait is done and the current thread can continue.

CountDownLatch usage scenarios

In the example above, everyone arrived at the restaurant and ate

2. Resources required for an operation are initialized

3. All threads that a service depends on are started…

Serve the grilled fish

Thanks to my friends and netizens for their support

Your concern and support is my biggest motivation ~

My public number: programmer seven cats

Comments and suggestions are welcome