多线程

等待其他线程执行完成 ,在执行

1/ countDownLathc

2/ join

3/futureTask

private final static ThreadFactory NAMED_THREAD_FACTORY = (Runnable r) -> new Thread(r, "CalAnayzeService-" + r.hashCode());
private final static ExecutorService EXECUTOR = new ThreadPoolExecutor(10, 20,
        120, TimeUnit.SECONDS,
        new LinkedBlockingQueue<>(), NAMED_THREAD_FACTORY);

List> futures = new ArrayList<>();
        for (int i = 0; i < smalls.size(); i++) {
            eventPatrolTypeInfo = smalls.get(i);
            result = new HomeIndex2SubOneMapYCVO();
            result.setXzbm(xzbm);
           
            futures.add(EXECUTOR.submit(new TaskSmallEventReport(result)));
        }
        for (int i = 0; i < futures.size(); i++) {
            try {
                resultList.add(futures.get(i).get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
class TaskSmallEventReport implements Callable {
    HomeIndex2SubOneMapYCVO targer ;
    public TaskSmallEventReport(HomeIndex2SubOneMapYCVO targer) {
        this.targer = targer;
    }
    @Override
    public HomeIndex2SubOneMapYCVO call() {
        return getSmallEventReport(targer);
    }
}

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