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
  • 相关阅读:
    读取列表下标
    字典dict详解
    使用mysql的长连接
    oauth授权协议的原理
    安装性能测试工具:sysbench和使用apache的ab
    发送邮件出现问题
    获取用户的真实ip
    清理代码的阅读笔记
    开发中三个经典的原则
    要干大事就不能把面子看得太重
  • 原文地址:https://www.cnblogs.com/yinlixin/p/7535403.html
Copyright © 2011-2022 走看看