zoukankan      html  css  js  c++  java
  • 多线程的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

  • 相关阅读:
    c#中跨线程调用windows窗体控件
    像职业选手样编码:地道Python
    数据挖掘笔记 第一章:引言
    SVN使用教程(基于SAE)
    常用的js函数
    linux服务之tuned
    PHP 开启短标签
    如叶梦想!
    分布式控制系统Git学习
    LabVIEW(十六):多列列表框控件
  • 原文地址:https://www.cnblogs.com/zheaven/p/12160226.html
Copyright © 2011-2022 走看看