• CountDownLatch的API讲解


    1.使用executor.awaitTermination(1, TimeUnit.HOURS);

    package com.dwz.utils;
    
    import java.util.Random;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class CountDownLatchExample1 {
        private static Random random = new Random(System.currentTimeMillis());
        private static ExecutorService executor = Executors.newFixedThreadPool(2);
        private static final CountDownLatch countDownLatch = new CountDownLatch(11);
        
        public static void main(String[] args) throws InterruptedException {
            //(1)查询数据
            int[] data = query();
            //(2)并行处理数据
            for(int i = 0; i < data.length; i++) {
                executor.execute(new SimpleRunnable(data, i));
            }
            //(3)
            executor.shutdown();
            executor.awaitTermination(1, TimeUnit.HOURS);
            System.out.println("all of work finish done.");
        }
        
        static class SimpleRunnable implements Runnable    {
            private final int[] data;
            private final int index;
            
            public SimpleRunnable(int[] data, int index) {
                this.data = data;
                this.index = index;
            }
            
            @Override
            public void run() {
                try {
                    Thread.sleep(random.nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                int value = data[index];
                if(value % 2 == 0) {
                    data[index] = value * 2;
                } else {
                    data[index] = value * 10;
                }
                System.out.println(Thread.currentThread().getName() + " finished.");
            }
        }
        
        private static int[] query() {
            return new int[] {0,1,2,3,4,5,6,7,8,9,10};
        }
    }

    2.使用countDownLatch.await();

    package com.dwz.utils;
    
    import java.util.Random;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.Executor;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class CountDownLatchExample1 {
        private static Random random = new Random(System.currentTimeMillis());
        private static ExecutorService executor = Executors.newFixedThreadPool(2);
        private static final CountDownLatch countDownLatch = new CountDownLatch(11);
        
        public static void main(String[] args) throws InterruptedException {
            //(1)查询数据
            int[] data = query();
            //(2)并行处理数据
            for(int i = 0; i < data.length; i++) {
                executor.execute(new SimpleRunnable(data, i));
            }
            //(3)
            countDownLatch.await();
            System.out.println("all of work finish done.");
        }
        
        static class SimpleRunnable implements Runnable    {
            private final int[] data;
            private final int index;
            
            public SimpleRunnable(int[] data, int index) {
                this.data = data;
                this.index = index;
            }
            
            @Override
            public void run() {
                try {
                    Thread.sleep(random.nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                int value = data[index];
                if(value % 2 == 0) {
                    data[index] = value * 2;
                } else {
                    data[index] = value * 10;
                }
                countDownLatch.countDown();
                System.out.println(Thread.currentThread().getName() + " finished.");
            }
        }
        
        private static int[] query() {
            return new int[] {0,1,2,3,4,5,6,7,8,9,10};
        }
    }

    3.latch.await();和latch.countDown();的混合使用

    package com.dwz.utils;
    
    import java.util.concurrent.CountDownLatch;
    
    public class CountDownLatchExample2 {
        public static void main(String[] args) {
            final CountDownLatch latch = new CountDownLatch(1);
            
            new Thread() {
                @Override
                public void run() {
                    System.out.println("Do some initial work.");
                    try {
                        Thread.sleep(1000);
                        latch.await();
                        System.out.println("Do other work.");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            
            new Thread() {
                @Override
                public void run() {
                    System.out.println("asyn prepare for some data.");
                    try {
                        Thread.sleep(2000);
                        System.out.println("data prepare done.");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        latch.countDown();
                    }
                }
            }.start();
        }
    }
  • 相关阅读:
    Python中的iterable该怎么理解?
    python中request中的params,data,json参数含义全解
    pycharm中查看变量值的方法汇总
    java多綫程
    spring boot 注解
    在Windows环境中使用Nginx, Consul, Consul Template搭建负载均衡和服务发现服务
    linux系统之四 常用工具
    linux系统之一 全连接与半连接队列
    linux系列之三 tcp缓冲区
    linux系统之二 文件句柄数
  • 原文地址:https://www.cnblogs.com/zheaven/p/13214133.html
Copyright © 2020-2023  润新知