CountDownLatch类的理解

package top.crossoverjie.cicada.base.bean;

import java.util.concurrent.CountDownLatch;

public class CountDown {
    private static Integer i = 0;
    final static CountDownLatch countDown = new CountDownLatch(1);

    public void odd() {
        while (i < 10) {
            if (i % 2 == 1) {
                System.out.println(Thread.currentThread().getName() + " - " + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                countDown.countDown();
            } else {
                try {
                    countDown.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void even() {
        while (i < 10) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + " - " + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                countDown.countDown();
            } else {
                try {
                    countDown.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {

        CountDown countDown = new CountDown();

        Thread t1 = new Thread(() -> countDown.odd(), "线程1");
        Thread t2 = new Thread(() -> countDown.even(), "线程2");

        t1.start();
        t2.start();
    }
}

运行结果
在这里插入图片描述

发布了15 篇原创文章 · 获赞 1 · 访问量 698

猜你喜欢

转载自blog.csdn.net/max1231ff/article/details/104019004