zoukankan      html  css  js  c++  java
  • 多线程分批处理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
  • 相关阅读:
    js幻灯片效果!
    构造函数和析构函数的简单说明
    ASP.NET接口的基础使用例子
    带预览图的js切换效果!
    在win7系统中安装sqlserver2005出现 [Microsoft][SQL Native Client]客户端不支持加密问题!
    Win7开启无线共享上网的方法
    C# 结构体 简明介绍
    C#访问修饰符简单说明
    C#不定长参数的使用
    研究了一下Google Ajax Search API, 给博客做了个样品
  • 原文地址:https://www.cnblogs.com/yinlixin/p/7535403.html
Copyright © 2011-2022 走看看