• 多线程的CountDown设计模式


    场景:控制逻辑按步骤执行

    Doug Lea的CountDownLatch实现方式

    package com.dwz.concurrency2.chapter15;
    
    import java.util.Random;
    import java.util.concurrent.CountDownLatch;
    import java.util.stream.IntStream;
    
    public class JDKCountDown {
        private final static Random random = new Random(System.currentTimeMillis());
        
        public static void main(String[] args) throws InterruptedException {
            final CountDownLatch latch = new CountDownLatch(5);
            
            System.out.println("准备多线程处理任务。。。");
            //第一步
            IntStream.rangeClosed(1, 5).forEach(i -> {
                new Thread(() -> {
                    System.out.println(Thread.currentThread().getName() + " is working.");
                    try {
                        Thread.sleep(random.nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //标记结束
                    latch.countDown();
                }, String.valueOf(i)).start();
            });
            
            //在等待所有任务结束
            latch.await();
            //第二步
            System.out.println("多线程任务全部结束,准备第二阶段任务");
            System.out.println("...................");
            System.out.println("FINISH");
        }
    }

    自定义一个CountDown

    package com.dwz.concurrency2.chapter15;
    
    public class CountDown {
        private final int total;
        private int counter = 0;
        
        public CountDown(int total) {
            this.total = total;
        }
        
        public void down() {
            synchronized (this) {
                this.counter++;
                this.notifyAll();
            }
        }
        
        public void await() throws InterruptedException {
            synchronized (this) {
                while (counter != total) {
                    this.wait();
                }
            }
        }
    }

    测试自定义CountDown

    package com.dwz.concurrency2.chapter15;
    
    import java.util.Random;
    import java.util.concurrent.CountDownLatch;
    import java.util.stream.IntStream;
    
    public class CustomCountDown {
        private final static Random random = new Random(System.currentTimeMillis());
        
        public static void main(String[] args) throws InterruptedException {
            final CountDown latch = new CountDown(5);
            System.out.println("准备多线程处理任务。。。");
            //第一步
            IntStream.rangeClosed(1, 5).forEach(i -> {
                new Thread(() -> {
                    System.out.println(Thread.currentThread().getName() + " is working.");
                    try {
                        Thread.sleep(random.nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //标记结束
                    latch.down();
                }, String.valueOf(i)).start();
            });
            
            //在等待所有任务结束
            latch.await();
            //第二步
            System.out.println("多线程任务全部结束,准备第二阶段任务");
            System.out.println("...................");
            System.out.println("FINISH");
        }
    
    
    }

    测试结果:

    准备多线程处理任务。。。
    2 is working.
    4 is working.
    1 is working.
    3 is working.
    5 is working.
    多线程任务全部结束,准备第二阶段任务
    ...................
    FINISH

  • 相关阅读:
    接口分类
    HTTPS和HTTP的主要区别
    协议类
    小程序
    Polyfill
    去重数组
    odoo 接口跨域请求报错
    docker-compose 自定义容器名称
    linux ssh 防暴力破解fail2ban
    odoo 知识点
  • 原文地址:https://www.cnblogs.com/zheaven/p/12160226.html
Copyright © 2020-2023  润新知