Examples of multi-threaded

Scenario is as follows

There are many brands, there are more products available in each brand, and now you want to filter out which brands do not have the goods.

Except for possible, there may not be shielded product user, so there is a filter logic.

For example, there is the brand under the Master Kong Master Kong tea, Master Kong instant noodles, etc., their daughter under the brand had nothing, it is imperative daughter filtered out, leaving the Master.

Filter thread follows

//得到品牌下商品的个数
class CountGoods implements Callable<Integer>{
    //分页参数
    PageRequest request;
    //品牌id
    Long brandId;
    //所有品牌列表,如果传入的品牌无效,就将此列表的该品牌去掉
    CopyOnWriteArrayList<Long> brandList;

    public CountGoods(PageRequest request, Long brandId, CopyOnWriteArrayList<Long> brandList){
        this.request = request;
        this.brandId = brandId;
        this.brandList = brandList;
    }

    @Override
    public Integer call() throws Exception{
        //获取所有品牌下商品
        Map<Long,List<Long>> brandSkuMap = getBrand(request.getBpin());
        //获取该品牌下的商品
        List<Long> subSkuList = brandSkuMap.get(fBrandId);
        //防止空数据
        if(subSkuList == null){
            return 0;
        }
        //对该品牌下的商品进行过滤
        List<GoodsInfo> goodsInfo= goodsRpc.synFilterSku(request.getBpin(), subSkuList);
        if(goodsInfo== null){
            return 0;
        }else{
            if(goodsInfo.size() == 0){
                brandList.remove(brandId);
            }
            return goodsInfo.size();
        }
    }
}

Multi-threaded filter

public List<Long> filterBrandList(PageRequest request, List<Long> IdList){
    List<Callable<Integer>> calls = new ArrayList();
    //过滤逻辑
    Iterator<Long> iterator = IdList.iterator();
    while(iterator.hasNext()){
        Long brandId =iterator.next();
        Callable call = new countId(request, brandId , (CopyOnWriteArrayList<Long>) IdList);
        calls.add(call);
    }

    ExecutorService cacheThreadPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
    List<Future<Integer>> callableResult = null;
    try{
        if(calls != null && calls.size() > 0){
            callableResult = cacheThreadPool.invokeAll(calls);
        }
    }catch (Exception e){
        e.printStackTrace();
        LogUtil.printErrorLog(log, "多线程过滤品牌出现异常");
    }finally {
        cacheThreadPool.shutdown();
    }
    return fontBrandIdList;
}

defect

There is no doubt that this code on a line to run out of the bug.

Very strange error, that is when the thread of execution, execution threads in the filtering wrong, but some are executed successfully.

Best not their sister, and finally came up with a question weekend, I did not seem to give it a blocking queue ......

Note, this thread pool queue blocking problem, need to be modified, so only 10 task over, more will be rejected. Modify the thread pool queue type can be blocked.

new SynchronousQueue<Runnable>()

This is no mean blocking queue.

amend as below:

ExecutorService cacheThreadPool = new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(100))

But there is a problem:

Note, so the new thread pool directly in the service method out is wrong, because it can not reuse thread. For example, two people visited this method, you are a new thread pool, did not use the same thread pool, but, if complicated by the high number of threads surge is likely to hang. You should be given a static thread pool, or write a separate thread pool bean, reuse everywhere.

Published 67 original articles · won praise 32 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_43751710/article/details/104608941