java 多线程:数据较多,分给多个线程并发处理

 1.  构建一个数据列表

 2. 将数据按照5个线程均分,每个线程拿到均分的数据

 3. 主线程通过等待5个处理数据线程处理好后最后停止

 4. 线程处理的结果信息返回   

 5. 比较不是并发的情况,并发5个线程是远快与一个线程处理的

public class ThreadTest2 {

    private  List msg = new ArrayList();
    private List  spliteList = null;


    public ThreadTest2(int  i){
        while(i>0){
            msg.add(i+"");
            i--;
        }
    }

    public List getMsg() {
        return msg;
    }

    public List>  spliteList(int groupSize){

        int length = msg.size();
        // 计算可以分成多少组
        int num =  length/groupSize;
        List> newList = new ArrayList<>(num);
        if(num == 0){
            newList.add(msg);
        }else{
            for (int i = 1; i <= groupSize; i++) {
                // 开始位置
                int fromIndex = (i-1)*num;
                // 结束位置
                int toIndex = i*num;
                if(i==groupSize){
                    toIndex = i*num+ length%groupSize;
                }

                newList.add(msg.subList(fromIndex,toIndex)) ;
            }
        }

        return  newList ;
    }

    public void customerMsg(List splitMsgList){
        for(String str : splitMsgList){
            System.out.println(Thread.currentThread().getName()+"----"+str);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        int groupSize = 5;
        //初始化数据
        ThreadTest2 test2 = new ThreadTest2(113);
        //拆分数据
        List>  tt = test2.spliteList(groupSize);
        CountDownLatch latch =  new CountDownLatch( groupSize );
        final ExecutorService exec = Executors.newFixedThreadPool(5);
        //多线程运行
        long startTime=System.currentTimeMillis();
        for(int ii=0; ii<5;ii++){
            exec.submit(new MyThread(tt.get(ii),latch,"Threadzcy"+ii));
        }
        latch.await();
        exec.shutdown();
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: "+(endTime-startTime)+"ms");

        long startTime1=System.currentTimeMillis();
        List  allMsgList = test2.getMsg();
        for(String str : allMsgList){

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        long endTime1=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: "+(endTime1-startTime1)+"ms");

    }



}

class MyThread  implements Runnable {
    private List  msgList = null;
    private CountDownLatch latch = null;
    private String name;

    public MyThread( List  msgList,CountDownLatch latch ,String name){
        this.msgList = msgList;
        this.latch = latch;
        this.name = name;
    }
    @Override
    public void run() {
        for(String str : msgList){
           // System.out.println(name+"----"+str);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        latch.countDown();
    }
}

结果:

程序运行时间: 2520ms
程序运行时间: 11315ms

你可能感兴趣的:(java-多线程,java)