package com.liulishuo.filedownloader.services;

import android.util.SparseArray;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Jacksgong on 9/25/15.
 */
class FileDownloadThreadPool {

    private SparseArray<FileDownloadRunnable> poolRunnables = new SparseArray<>();

    private ExecutorService threadPool = Executors.newFixedThreadPool(3);

    public void execute(FileDownloadRunnable runnable) {
        runnable.onResume();
        threadPool.execute(runnable);
        poolRunnables.put(runnable.getId(), runnable);

        if (mIgnoreCheckTimes >= CHECK_THRESHOLD_VALUE) {
            checkNoExist();
            mIgnoreCheckTimes = 0;
        } else {
            mIgnoreCheckTimes++;
        }
    }


    private final int CHECK_THRESHOLD_VALUE = 600;
    private int mIgnoreCheckTimes = 0;

    private synchronized void checkNoExist() {
        SparseArray<FileDownloadRunnable> newRunnables = new SparseArray<>();
        for (int i = 0; i < poolRunnables.size(); i++) {
            final int key = poolRunnables.keyAt(i);
            final FileDownloadRunnable runnable = poolRunnables.get(key);
            if (runnable.isExist()) {
                newRunnables.put(key, runnable);
            }
        }
        poolRunnables = newRunnables;

    }

    public boolean isInThreadPool(final int downloadId) {
        final FileDownloadRunnable runnable = poolRunnables.get(downloadId);
        return runnable != null && runnable.isExist();
    }
}

@@ -316,24 +316,55 @@
 Array%3C%3E();%0A%0A
+    // TODO %E5%AF%B9%E7%94%A8%E6%88%B7%E5%BC%80%E6%94%BE%E7%BA%BF%E7%A8%8B%E6%B1%A0%E5%A4%A7%E5%B0%8F%EF%BC%8C%E5%85%A8%E5%B1%80%E5%B9%B6%E8%A1%8C%E4%B8%8B%E8%BD%BD%E6%95%B0%0A
     private 

添加 todo