Spring Boot---(7)SpringBoot多线程

前提:

日常开发,有很多场景会使用到多线程,比如,我们解析Excel,如果解析出一个3万条数据的Excel表格,需要两部:

1.我们需要先异步解析出所有的数据,前面写过了如何异步处理任务(Spring Boot---(4)SpringBoot异步处理任务);

2.然后再多线程去处理业务或者插入到数据库;

这里,讲解一下,如何使用多线程,如何调用回调函数

1.引入pom.xml的依赖

由于后面要用到Guava包下的东西,所以这里先引入一下。

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>18.0</version>
</dependency>

2.controller

这里模拟10万条数据,然后多线程处理。

package com.jd.concurrent;

import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * author:lightClouds917
 * date:2018/1/22
 * description:模拟多线程处理
 */
@RestController
@RequestMapping("con")
public class ConController {
    private final static Logger logger = LoggerFactory.getLogger(Logger.class);

    @RequestMapping(value = "test1",method = RequestMethod.GET)
    public String test1(){
        try {
            //10万条数据
            List<String> list = new ArrayList<>();
            List<String> list2 = new ArrayList<>();

            for(int i = 1; i <= 100000;i++){
                list.add("test:"+i);
            }

            //每条线程处理的数据尺寸
            int size = 250;
            int count = list.size()/size;
            if(count*size != list.size()){
                count ++;
            }
            int countNum = 0;
            final CountDownLatch countDownLatch =  new CountDownLatch(count);
            ExecutorService executorService = Executors.newFixedThreadPool(8);
            ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
            while (countNum < list.size()){
                countNum += size;
                ConCallable callable = new ConCallable();
                //截取list的数据,分给不同线程处理
                callable.setList(ImmutableList.copyOf(list.subList(countNum - size,countNum < list.size() ? countNum : list.size())));
                ListenableFuture listenableFuture = listeningExecutorService.submit(callable);
                Futures.addCallback(listenableFuture, new FutureCallback<List<String>>() {
                    @Override
                    public void onSuccess(List<String> list1) {
                        countDownLatch.countDown();
                        list2.addAll(list1);
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                        countDownLatch.countDown();
                        logger.info("处理出错:",throwable);

                    }
                });
            }
            countDownLatch.await(30, TimeUnit.MINUTES);
            logger.info("符合条件的返回数据个数为:"+list2.size());
            logger.info("回调函数:"+list2.toString());
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "正在处理......";

    }
}

3.线程任务处理类

package com.jd.concurrent;

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

/**
 * author:lightClouds917
 * date:2018/1/22
 * description:业务处理
 */
public class ConCallable implements Callable {
    private List<String> list;

    @Override
    public Object call() throws Exception {
        List<String> listRe = new ArrayList<>();
        for(int i = 0;i < list.size();i++){
            //含有‘4599’的字符串都返回
            if(list.get(i).contains("4599")){
                listRe.add(list.get(i));
            }
        }
        return listRe;
    }

    public void setList(List<String> list) {
        this.list = list;
    }
}

4.返回结果:

符合条件的返回数据个数为:20
回调函数:[test:4599, test:14599, test:24599, test:34599, test:44599, test:45990, test:45991, test:45992, test:45993, test:45994, test:45995, test:45996, test:45997, test:45998, test:45999, test:54599, test:64599, test:74599, test:84599, test:94599]

这里面,出现了:CountDownLatch,ListenableFuture,ImmutableList等的使用,如果不清晰如何使用,请参考如下: (未完,待续.......)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券