多线程分批处理list内的值

Posted apache-xinge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程分批处理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();  
    }  
} 

 



 

以上是关于多线程分批处理list内的值的主要内容,如果未能解决你的问题,请参考以下文章

C#多线程如何处理数据

Query the commission contract which without cost center(分批处理数据,多线程异步调用)

线程学习知识点总结

线程池和CountDownLatch配合使用,大数据量批量多次处理

并发实战:多线程处理任务,结束后,执行后续操作

多线程分批发送消息