首页 > 解决方案 > 如何从 Threadpool 执行器获取回调以完成 JobService

问题描述

我的场景是我需要下载 30 个 zip 文件,每个 3 MB,并在用户第一次登录时解压缩所有下载的文件。根据OREO后台限制,现在我正在安排一个工作来完成这个任务。但这里的问题是,如果我按顺序下载所有 zip,则需要时间。我需要这个任务在后台并行运行,我正在使用线程池执行器来运行线程。但是,一旦我使用 Thread pool Executor,我就无法弄清楚如何停止我的工作。我怎样才能得到一个回调,说所有的 zip 文件都已下载。安排工作是做后台的正确方法还是我应该使用工作管理器我需要保证这个任务执行到最后?

下面是线程池执行器类:

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolExecutor {
    private static MyThreadPoolExecutor MyThreadPoolExecutor;
    private static int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
    private static final TimeUnit KEEP_ALIVE_TIME_UNIT;
    private final BlockingQueue<Runnable> mDownloadWorkQueue;
    private final ThreadPoolExecutor mDownloadThreadPool;
    private static final int CORE_POOL_SIZE = 8;
    private static final int MAXIMUM_POOL_SIZE = 8;
    private static final int KEEP_ALIVE_TIME = 1;
    private Handler mHandler;

    // A static block that sets class fields
    static {

        // The time unit for "keep alive" is in seconds
        KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;

        // Creates a single static instance of PhotoManager
        MyThreadPoolExecutor = new MyThreadPoolExecutor();
    }

    private MyThreadPoolExecutor() {

        mDownloadWorkQueue = new LinkedBlockingQueue<Runnable>();

        mDownloadThreadPool = new ThreadPoolExecutor(NUMBER_OF_CORES * 2, NUMBER_OF_CORES * 2,
                KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, mDownloadWorkQueue);

        mHandler = new Handler(Looper.getMainLooper()) {

            /*
             * handleMessage() defines the operations to perform when the
             * Handler receives a new Message to process.
             */
            @Override
            public void handleMessage(Message inputMessage) {

            }
        };
    }


    public static MyThreadPoolExecutor getInstance() {
        return MyThreadPoolExecutor;
    }


    public void addThread(Runnable runnable){
        mDownloadWorkQueue.add(runnable);
    }

    public void execute(Runnable runnable){
            MyThreadPoolExecutor.mDownloadThreadPool.execute(runnable);

    }


}

下面是我的可运行代码:

public class SimpleThread implements Runnable {
    private String file_url;

    public SimpleThread( String url) {
        this.file_url = url;
    }

    @Override
    public void run() {
        try{

            URL url = new URL(file_url.replaceAll(" ", "%20"));
            InputStream fileInputStream = url.openStream();
            if(fileInputStream != null) {
                lessonmediaSaver.save(fileInputStream);
                String source = "path";
                String destination = "destinationpath";
                 new UnzipUtility().unzip(source,destination);

                System.out.println("Saving Zip --> ");
            }else{
                System.out.println("Zip not saved --> ");

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

这就是我执行作业服务代码的方式:

for(MyObject myObject:myObjectArray){
                        SimpleThread simpleThread = new SimpleThread(myObject.getFileUrl());
                        IstarThreadPoolExecutor.getInstance().execute(simpleThread);
                    }

标签: androidandroid-8.0-oreothreadpoolexecutor

解决方案


推荐阅读