多线程分批处理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 @ 2017-09-17 13:57  apache-xinge  阅读(840)  评论(0编辑  收藏  举报