Using CountDownLatch to solve program performance problems

Preface:

2003~

problem description:
existing store Id collection [List < Integer >], and execute static method [staticizeIndexBySubsite (Integer subId)], want to open 3 threads to let staticizeIndexBySubsite method consume store collection List, entry time is short plus construction period is urgent, inexperienced I can"t think of a thread-safe solution at a time, so I can only go here to find out:)

< hr >

Code:

public synchronized void staticizeIndexBySubsites(List<Integer> subsiteIds) {
    if(CollectionUtils.isNotEmpty(subsiteIds)){
        for(Integer id : subsiteIds){
            //
            staticizeIndexBySubsite(id);
        }
    }
}


@Override
public void staticizeIndexBySubsite(Integer subsiteId) {
    if(NumberUtils.isEmpty(subsiteId)) return;
    try{
        Boolean ischinese;
        OverSeaHomeVO overSeaHomeVO;
        HomeNewVO homeNewVO;
        HomeNewVO wxHomeNewVO;
        String fileName;
        for(int y = 0 ; y<language.length;yPP){
            ischinese = language[y];
            fileName = subsiteId+"-"+(language[y]==true?"zh-CN":"en")+".json";
            try {
                SubsiteDTO subsiteDTO = subSiteService.get(subsiteId);
                if(subsiteDTO != null && subsiteDTO.getStatus() == 1){
                    if(subsiteDTO.isIsGlobal()){//
                        overSeaHomeVO = mobileStaticIndexService.getOverseaIndexBySubsite(subsiteId, ischinese, MultipleChannelsConstants.IOS_CHANNEL);
                        if(overSeaHomeVO != null){
                            writeAppIndex(successResult(overSeaHomeVO),parentRealPath,fileName);
                            writeAppIndex(successResult(overSeaHomeVO),wechatIndexReaPath,fileName);
                        }
                    }else{//
                        homeNewVO = mobileStaticIndexService.getHomeNewBySubsite(MobileSiteStructureIdsConstant.MOBILE_NEW_SITE_STRUCTURE_ID_HOME, subsiteId, ischinese, MultipleChannelsConstants.IOS_CHANNEL);
                        if(homeNewVO != null){
                            writeAppIndex(successResult(homeNewVO),parentRealPath,fileName);
                        }
                        // 
                        wxHomeNewVO = mobileStaticIndexService.getHomeNewBySubsite(MobileSiteStructureIdsConstant.MOBILE_SITE_STRUCTURE_ID_WX_HOME, subsiteId, ischinese, MultipleChannelsConstants.WEIXIN_CHANNEL);
                        if(wxHomeNewVO != null){
                            writeAppIndex(successResult(wxHomeNewVO),wechatIndexReaPath,fileName);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("MobileStaticIndexProcess error" + subsiteId+"***"+ischinese);
            }
        }
    }catch(Exception e){
        e.printStackTrace();
        log.error("json"+e.fillInStackTrace());
    }
}
Mar.15,2021

answer your own questions.

final static int threadNum = 3; //
static CountDownLatch countDownLatch = null;
@Override
public synchronized void staticizeIndexBySubsites(List<Integer> subsiteIds) throws Exception{
    if(CollectionUtils.isNotEmpty(subsiteIds)){
        countDownLatch = new CountDownLatch(threadNum);
        int length = subsiteIds.size();
        int tl = length % threadNum == 0 ? length / threadNum : (length/ threadNum + 1);
        for (int i = 0; i < threadNum; iPP) {
            int end = (i + 1) * tl;
            HandleThread thread = new HandleThread(subsiteIds, i * tl, end > length ? length : end);
            thread.start();
        }
        countDownLatch.await();
    }
}

class HandleThread extends Thread {
    private List<Integer> data;
    private int start;
    private int end;
    public HandleThread(List<Integer> data, int start, int end) {
        this.data = data;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        List<Integer> subList = data.subList(start, end);
        for(Integer item:subList){
            staticizeIndexBySubsite(item);
        }
        countDownLatch.countDown();
        System.out.println(":"+Thread.currentThread().getId()+""+subList.size()+":"+((System.currentTimeMillis() - startTime)/1000) + "s");
    }
}

I have nothing to do. I wrote one for you.

public class Test0530 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        List<String> shops = new ArrayList<>();
        for (int i = 1; i <= 200; iPP) {
            shops.add(i+"");
        }

        //  10
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        List<Future<String>> futures = new ArrayList<>();

        long start = System.currentTimeMillis();
        for (String shop : shops) {
            Future<String> future = executorService.submit(() -> {
                try {
                    // 5
                    System.out.println("init " + shop);
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return shop;
            });
            futures.add(future);
        }
        
        for (Future<String> future : futures) {
            System.out.println("done " +future.get());
        }

        long end = System.currentTimeMillis();

        System.out.println("used time " + (end-start) + " ms");
    }
}
Menu