並行執行任務

需求

在app列表首頁,展示多個item,並有分頁;而每個item裏後臺都會調用一個http請求,判斷當前item的狀態

分析

爲了更好的用體驗,無疑需要使用多線程並行處理http請求,而且還需要拿到每個線程的執行結果. 上面的分析,有兩個問題需要解決: 1. 如何創建線程池 2. 如何拿到所有線程的執行結果 對於第一個問題,還是很好解決的,使用併發包( java.util.concurrent)下面的ThreadPoolExecutor類創建線程池,阿里巴巴Java開發手冊上推薦使用該類創建線程池:

,根據統計,該首頁的qps最大爲3以及服務器的配置後,線程池創建如下:

protected static class ThreadFactory {
        static ThreadPoolExecutor executor = new ThreadPoolExecutor(ThreadNumEnum.CORE_POOL_SIZE.getNum(), ThreadNumEnum.MAX_IMUM_POOL_SIZE.getNum(), TimeOutEnum.FiveSecond.getSeconds(), TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>(),new ThreadPoolExecutor.DiscardPolicy());
        private ThreadFactory() {

        }
        public static ExecutorService getThreadPool() {
            return executor;
        }
    }

如何能拿到線程的執行結果呢,傳統的Thread無法拿到執行結果,由於run方法無返回值,通過ThreadPoolExecutor類圖發現:

繼承了AbstractExecutorService、ExecutorService,對ExecutorService中的invokeAll方法產生極大的興趣,仔細閱讀註釋,其實這個方法用來並行執行任務:

/**
     * Executes the given tasks, returning a list of Futures holding
     * their status and results
     * when all complete or the timeout expires, whichever happens first.
     * {@link Future#isDone} is {@code true} for each
     * element of the returned list.
     * Upon return, tasks that have not completed are cancelled.
     * Note that a <em>completed</em> task could have
     * terminated either normally or by throwing an exception.
     * The results of this method are undefined if the given
     * collection is modified while this operation is in progress.
     *
     * @param tasks the collection of tasks
     * @param timeout the maximum time to wait
     * @param unit the time unit of the timeout argument
     * @param <T> the type of the values returned from the tasks
     * @return a list of Futures representing the tasks, in the same
     *         sequential order as produced by the iterator for the
     *         given task list. If the operation did not time out,
     *         each task will have completed. If it did time out, some
     *         of these tasks will not have completed.
     * @throws InterruptedException if interrupted while waiting, in
     *         which case unfinished tasks are cancelled
     * @throws NullPointerException if tasks, any of its elements, or
     *         unit are {@code null}
     * @throws RejectedExecutionException if any task cannot be scheduled
     *         for execution
     */
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                  long timeout, TimeUnit unit)
        throws InterruptedException;

到這裏,第二個問題的解決思路已經有了.

編碼實現

invokeAll方法的入參分別爲任務列表list、超時時間、時間單位,所以首先我們需要創建任務列表: List<BasicUserFilter>list=newArrayList<>(); 超時時間爲每個FutureTask執行超時時間,這裏設置成3s,這裏的3s超時時間是針對的所有tasks,而不是單個task的超時時間,如果超時,會取消沒有執行完的所有任務,並拋出超時異常,源碼如下:

for (int i = 0; i < size; i++) {
                execute((Runnable)futures.get(i));
                nanos = deadline - System.nanoTime();
                if (nanos <= 0L)
                    return futures;
            }

            for (int i = 0; i < size; i++) {
                Future<T> f = futures.get(i);
                if (!f.isDone()) {
                    if (nanos <= 0L)
                        return futures;
                    try {
                        f.get(nanos, TimeUnit.NANOSECONDS);
                    } catch (CancellationException ignore) {
                    } catch (ExecutionException ignore) {
                    } catch (TimeoutException toe) {
                        return futures;
                    }
                    nanos = deadline - System.nanoTime();
                }
            }

BasicUserFilter需要實現Callable接口,因爲在方法call裏能拿到線程的執行結果, 下面就是並行執行任務了:

ExecutorService executor = ThreadFactory.getThreadPool();
        List<XXX> userFilterDtoList = new ArrayList<>();
        try {
            List<Future<XXX>> futureList = executor.invokeAll(list, TimeOutEnum.FourSecond.getSeconds(), TimeUnit.MILLISECONDS);
            futureList.stream().forEach(p -> {
                try {
                    Future<XXX> filterDtoFuture = p;
                   //拿到線程執行結果  
                   userFilterDtoList.add(filterDtoFuture.get());
                } catch (InterruptedException e) {
                    logger.error(e.getMessage(), e);
                } catch (ExecutionException e) {
                    logger.error(e.getMessage(), e);
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            });
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

關於多線程筆者還有很多需要去學習,上面是一個工作筆記,關於invokeAll的執行流程、神奇的Future模式,感興趣的可以閱讀源碼就能找到答案.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章