CountDownLatch-倒计时锁

原理

适合总分任务,这个锁主要适用情景,要求某些操作全部执行完成才能继续执行后面操作,这时候就可以适用cdl来计数,每次执行完一个操作就减一,计数为0后表示全部线程执行完毕。

注意

cdl限定了总数,一般来说,如果要让全部操作执行完,总数和操作数相等即可;如果总数大于操作数,操作将会永远消耗不完而卡住,如果总数小于操作数,操作不能等待全部执行完,而是执行完指定数量放行,但是执行下一步操作时,并行的线程依然会对公用变量进行操作,接下去的操作不会只有总数的数量,一般会大于这个数量。

代码

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        CountDownLatch cdl = new CountDownLatch(10000);
        for (int i = 1; i <= 10000 ; i++) {
            final int index = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    synchronized (CountDownLatchSample.class){
                        try {
                            count++;
                            System.out.println(Thread.currentThread().getName()+"--"+count);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            cdl.countDown();
                        }
                    }
                }
            });
        }
        try {
            cdl.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(count);
并行测试
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        CountDownLatch cdl = new CountDownLatch(5);
        for (int i = 1; i <= 10; i++) {
            final int index = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    synchronized (CountDownLatchSample.class) {
                        try {
                            count++;
                            System.out.println("pre+++"+Thread.currentThread().getName() + "--" + count+"=="+cdl.getCount());
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            cdl.countDown();
                            System.out.println(Thread.currentThread().getName() + "--" + count+"=="+cdl.getCount());
                        }
                    }
                }
            });
        }
        try {
            cdl.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 20; i++) {
            System.out.println(count);
        }
结果:
pre+++pool-1-thread-1--1==5
pool-1-thread-1--1==4
pre+++pool-1-thread-1--2==4
pool-1-thread-1--2==3
pre+++pool-1-thread-1--3==3
pool-1-thread-1--3==2
pre+++pool-1-thread-1--4==2
pool-1-thread-1--4==1
pre+++pool-1-thread-1--5==1
pool-1-thread-1--5==0
pre+++pool-1-thread-1--6==0
pool-1-thread-1--6==0
pre+++pool-1-thread-1--7==0
pool-1-thread-1--7==0
pre+++pool-1-thread-1--8==0
pool-1-thread-1--8==0
8
8
8
8
9
9
9
9
9
9
9
pre+++pool-1-thread-2--9==0
9
pool-1-thread-2--9==0
9
10
10
10
pre+++pool-1-thread-3--10==0
pool-1-thread-3--10==0
10
10
10
10

 

 

 

posted @ 2022-10-12 13:29  求道之愚者  阅读(51)  评论(0)    收藏  举报