• 多线程分批处理list内的值


    package three;
    
    import java.util.LinkedList;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    
    /**
     * 
     * 这个方法没有返回值,不能接到最后的处理结果
     *
     */
    public class MyExecutors {
        private static LinkedList<Integer> list = new LinkedList<Integer>();
    
        public static void main(String[] args) throws InterruptedException {
            for (int i = 0; i < 100; i++) {
                list.add(i);
            }
            
            // 创建线程池
            ExecutorService executorService = Executors.newFixedThreadPool(90);
            for (Integer every : list) {
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName() + "   " + every);
                    }
                });
            }
            System.out.println("------------------------------------------");
            // 任务执行完毕后结束线程
            executorService.shutdown();
        }
    }




    //---------------------------------------------------下面的这个类是智慧的网友写的,有返回值--------------------------------------------------------------------------
    package four;
    import java.util.ArrayList;  
    import java.util.List;  
    import java.util.concurrent.Callable;  
    import java.util.concurrent.ExecutorService;  
    import java.util.concurrent.Executors;  
    import java.util.concurrent.Future;  
      
    public class CunsumerList {  
        public static void main(String[] args) {  
            try {  
                List<String> list = new ArrayList<>();  
                for (int i = 0; i < 100; i++) {  
                    list.add(i + ",");  
                }  
                  
                System.out.println(new CunsumerList().list2Str(list, 5));  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        /**
         * 这个能知道线程的处理结果,因为有返回值
         * @param list
         * @param nThreads
         * @return
         * @throws Exception
         */
        public String list2Str(List<String> list, final int nThreads) throws Exception {  
            if (list == null || list.isEmpty()) {  
                return null;  
            }  
              
            StringBuffer ret = new StringBuffer();  
      
            int size = list.size();  
            ExecutorService executorService = Executors.newFixedThreadPool(nThreads);  
            List<Future<String>> futures = new ArrayList<Future<String>>(nThreads);  
            
            
            for (int i = 0; i < nThreads; i++) {
                final List<String> subList = list.subList(size / nThreads * i, size / nThreads * (i + 1));  
                Callable<String> task = new Callable<String>() {  
                    @Override  
                    public String call() throws Exception { 
                        System.out.println(Thread.currentThread().getName());
                        StringBuffer sb = new StringBuffer();  
                        for (String str : subList) {  
                            sb.append(str);  
                        }  
                        return sb.toString();  
                    }  
                };  
                futures.add(executorService.submit(task));  
            }  
            for (Future<String> future : futures) {  
                ret.append(future.get());  
            }  
            executorService.shutdown();  
              
            return ret.toString();  
        }  
    } 
    
    
    


    spring集成mongodb
  • 相关阅读:
    第四篇 kubernetes 部署EFK日志平台
    青云KubeSphere 容器平台部署教程
    第三篇 kubernetes使用Operator部署Prometheus监控
    第二篇 kubernetes 集群部署 Traefik-ingress
    第一篇 kubernetes V1.19.4 部署教程
    OpenShift强大的oc set命令详解
    kubernetes集群调度策略及调度原理
    MAC 环境变量的管理
    hackindosh 折腾
    RactNative 框架 Hippy
  • 原文地址:https://www.cnblogs.com/yinlixin/p/7535403.html
Copyright © 2020-2023  润新知