多线程分批处理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();  
    }  
} 
复制代码

 



复制代码

 

posted @   apache-xinge  阅读(848)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示