스 레 드 탱크 다 중 스 레 드 병행 처리 대량 데이터

3777 단어 Java


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTool {

    //          
    private int singleCount;
    //       
    private int listSize;
    //      
    private int runSize;
    //      
    private List list;
    //   
    private CountDownLatch begin,end;
    //   
    private ExecutorService executorService;
    //  
    private CallBack callBack;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public ThreadPoolTool(int singleCount, List list){
        this.singleCount = singleCount;
        this.list = list;
        if (list != null){
            this.listSize = list.size();
            this.runSize = (this.listSize/this.singleCount) + 1;
        }
    }

    public void excute() throws InterruptedException {
        executorService = Executors.newFixedThreadPool(runSize);
        begin = new CountDownLatch(1);
        end = new CountDownLatch(runSize);
        //    
        int startIndex = 0;
        int endIndex = 0;
        List newList = null;
        for (int i = 0; i < runSize; i++) {
            //           
            if (i < (runSize - 1)){
                startIndex = i * singleCount;
                endIndex = (i + 1) * singleCount;
                newList = list.subList(startIndex,endIndex);
            }else {
                startIndex = i * singleCount;
                endIndex = listSize;
                newList = list.subList(startIndex,endIndex);
            }
            //         
            MyThread myThread = new MyThread(newList, begin, end) {
                @Override
                public void method(List list) {
                    callBack.method(list);
                }
            };
            //    
            executorService.execute(myThread);
        }
        //     
        begin.countDown();
        end.await();
        //     
        executorService.shutdown();
    }

    //     
    public abstract class MyThread implements Runnable{

        private List list;
        private CountDownLatch begin,end;

        public MyThread(List list, CountDownLatch begin, CountDownLatch end){
            this.list = list;
            this.begin = begin;
            this.end = end;
        }

        @Override
        public void run() {
            try {
                //    
                method(list);
                begin.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                //     
                end.countDown();
            }
        }
        public abstract void method(List list);
    }

    //      
    public interface CallBack{
        public void method(List list);
    }


    public static void main(String[] agrs){
        List list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add("hello"+i);
        }
        ThreadPoolTool tool = new ThreadPoolTool(100,list);
        tool.setCallBack(new CallBack() {
            @Override
            public void method(List list) {
                for (int i = 0; i < list.size(); i++) {
                    System.out.print(Thread.currentThread().getId()+":"+list.get(i)+" ");
                }
                System.out.println();
            }
        });
        try {
            tool.excute();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

좋은 웹페이지 즐겨찾기