记一次 CompletableFuture 实际使用场景

2023-09-08  本文已影响0人  smart_dev

如果忘记使用方法可参考这篇文章

项目背景

那实际问题就转化成了: 如何去开辟多线程(几个为好这里不讨论)同时需要等待结果都回来

优化方案

采用多线程下载静态图片,并且等全部任务执行完毕后,再继续往下执行。

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

public class ImageDownloadManager {
    private static final String TAG = ImageDownloadManager.class.getSimpleName();
    ExecutorService mExecutor;


    public ImageDownloadManager() {
        mExecutor = ExecutorManager.xxxx();
    }

    public List<PictureItem> download(Context context, List<PictureItem> imageList) {
        List<PictureItem> copyList = new CopyOnWriteArrayList<>(imageList);
        List<CompletableFuture> futureList = new ArrayList<>();
        Log.d(TAG, "Start download for all images.");

        for (PictureItem item : imageList) {
          
                CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(new Supplier<Boolean>() {
                    @Override
                    public Boolean get() {
                        boolean isValid = XXXUtils.preLoadImage(xxxx);
                        if (!isValid) {
                            copyList.remove(item);
                        }
                        Log.d(TAG, "A download end, isValid:" + isValid + " url: " + xxx);
                        return isValid;
                    }
                }, mExecutor);
                futureList.add(future);
            }
        

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()]));
        voidCompletableFuture.join();
        Log.d(TAG, "End download for all images. download total count = " + futureList.size()
                + " ,download fail count = " + (imageList.size() - copyList.size()));
        return new ArrayList<>(copyList);
    }
}

优化结果

整体优化后速度提升了 100% - 140%

项目方案复用需考虑的因素

  1. 线程个数与任务个数的选择协调
  2. 任务类型,前台还是后台,是否紧急
上一篇下一篇

猜你喜欢

热点阅读