如何在 Android 中管理可运行任务队列

Var*_*pal 5 java multithreading android design-patterns

我有多个任务/可运行(即从互联网下载图像),这些任务是当用户滚动 Android 应用程序中的列表时生成的。

我无法控制一次生成多少个任务/Runnable,这可能是 100 个。但我只想并行执行 n(10) 个任务。因此,我计划构建一个设计,一旦生成新任务/可运行任务,它将被添加到队列 ( List<Runnable>) 中,并且通过Executors.newFixedThreadPool(10),我将仅并行执行前 10 个可运行任务。现在,一旦任务/Runnable 完成,我应该能够将它们从队列 ( List<Runnable>) 中删除,并且应该能够执行 FIFO 中队列中的新任务/Runnable。

我为这个设计开设了两个课程。第一个ExecutorManager是单例类,管理 10 个并行任务的执行,第二个是ImageDownloader实现runnable并负责下载图像的类。我不确定通知ExecutorManager任务/下载已完成并且可以从队列中执行新任务的最佳方式是什么。我遵循 FIFO,因此我总是从队列中的前 10 个任务开始执行,但是我如何知道哪个任务已完成以及哪个任务要从队列中删除?

public class ImageDownloader implements Runnable{

    DownloadListener mDownloadListener;

    public ImageDownloader(DownloadListener mDownloadListener, String URL){
        this.mDownloadListener = mDownloadListener;
    }


    @Override
    public void run() {
        //Download the Image from Internet
            //ToDo
        //if Success in download
            mDownloadListener.onDownloadComplete();
        //if Error in download
            mDownloadListener.onDownloadFailure();
        //Inform the Executor Manager that the task is complete and it can start new task
         incrementCount();
    }

    private static synchronized void incrementCount(){
        ExecutorManager.getInstance().OnTaskCompleted();// is there a better way to do it
    }
}



public class ExecutorManager {

    private static ExecutorManager Instance;
    ExecutorService executor = Executors.newFixedThreadPool(Constants.NumberOfParallelThread);
    ArrayList<Runnable> ExecutorQueue = new ArrayList<Runnable>();
    int ActiveNumberOfThread = 0;

    private ExecutorManager(){
    }

    public static ExecutorManager getInstance(){

        if(Instance==null){
            Instance = new ExecutorManager();

        }
        return Instance;
    }

    private void executeTask(){
        if(ExecutorQueue.size()>0 && ActiveNumberOfThread < Constants.NumberOfParallelThread){
            ++ActiveNumberOfThread;
            executor.execute(ExecutorQueue.get(0));//Execute the First Task in Queue
        }
    }

    public void enQueueTask(Runnable Task){

        ExecutorQueue.add(Task);
        executeTask();
    }

    public void removeFromQueue(){
        //How to know, which task to remove?
        ExecutorQueue.remove(0);
    }

    public void OnTaskCompleted(){
        --ActiveNumberOfThread;
        removeFromQueue();
        executeTask();
    }

}
Run Code Online (Sandbox Code Playgroud)

Dee*_*eeV 4

嗯,你很幸运。您根本不必告诉 ExecutorManager 任何内容。一个ExecutorServiceBlockingQueue把手的人为你排队。您所要做的就是将其提交Runnable给ExecutorService。它会坚持下去。如果有任何打开的线程,它将立即运行。否则,它将等待其中一个Runnables完成执行。完成后,将进行下一个。

如果你查看Executors#newFixedThreadPool的源代码,它实际上只是创建一个ThreadPoolExecutor由如下nThreads支持的线程:LinkedBlockingQueue

public static ExecutorService newFixedThreadPool(int nThreads) {
         return new ThreadPoolExecutor(nThreads, nThreads,
                                       0L, TimeUnit.MILLISECONDS,
                                       new LinkedBlockingQueue<Runnable>());
} 
Run Code Online (Sandbox Code Playgroud)