java线程池处理数据2

package cn.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.collections.CollectionUtils;

import com.google.common.collect.Lists;

import cn.demo.thead.TestThread;

public class TheardDemo {

     static ExecutorService executorService = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        
        List numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8);
        List result = new ArrayList<>();

        List> partList = Lists.partition(numList, 3);

        if (!CollectionUtils.isEmpty(partList)) {
            for (List list : partList) {
                //System.out.println("list"+list);
                Future> queryFuture = executorService.submit(new TestThread(list));
                try {
                    result.addAll(queryFuture.get());
                    //System.out.println("多线程处理后"+result);
                } catch (InterruptedException e) {
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
package cn.demo.thead;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import lombok.Data;

@Data
public class TestThread implements Callable> {

    private List list;

    
    
    @Override
    public List call() throws Exception {
        List results = new ArrayList();
        for (Integer t : list) {
            Integer j = t+1000;
            results.add(j);
            //System.out.println(j);
        }
        System.out.println(results);
        return results;
    }

    
    
    public TestThread(List list) {
        this.list = list;
    }
}

你可能感兴趣的:(java线程池处理数据2)