• 多线程的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

  • 相关阅读:
    背包问题
    floyed算法
    读Windows编程
    PB串口编程资料(转)
    读TCP-IP详解卷1:协议(1)
    Oracle把两个空格以上的空格,替换为两个空格
    PB中multieditline空间的“~r~n"转"~n"
    PB中掉用Run以后,等Run的程序关闭以后才会执行后边的语句
    一个关于生成随机数的算法
    英语词根
  • 原文地址:https://www.cnblogs.com/zheaven/p/12160226.html
Copyright © 2020-2023  润新知