diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 9e6fde29..59bd9de3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -25,6 +25,7 @@ import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.IDownloadSchedulers; import com.arialyy.aria.core.task.Task; import com.arialyy.aria.core.task.TaskFactory; +import com.arialyy.aria.util.Configuration; /** * Created by lyy on 2016/8/17. @@ -106,7 +107,7 @@ public class DownloadTaskQueue implements ITaskQueue { } @Override public void reTryStart(Task task) { - if (task == null){ + if (task == null) { Log.w(TAG, "重试下载失败,task 为null"); return; } @@ -126,7 +127,32 @@ public class DownloadTaskQueue implements ITaskQueue { } @Override public void setDownloadNum(int downloadNum) { + //原始长度 + int size = Configuration.getInstance().getDownloadNum(); + int diff = downloadNum - size; + if (size == downloadNum){ + Log.d(TAG, "设置的下载任务数和配置文件的下载任务数一直,跳过"); + return; + } + //设置的任务数小于配置任务数 + if (diff <= -1 && mExecutePool.size() >= size) { + for (int i = 0, len = Math.abs(diff); i < len; i++) { + Task eTask = mExecutePool.pollTask(); + if (eTask != null) { + stopTask(eTask); + } + } + } mExecutePool.setDownloadNum(downloadNum); + if (diff >= 1) { + for (int i = 0; i < diff; i++) { + Task nextTask = getNextTask(); + if (nextTask != null + && nextTask.getDownloadEntity().getState() == DownloadEntity.STATE_WAIT) { + startTask(nextTask); + } + } + } } @Override public Task createTask(Object target, DownloadEntity entity) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/CachePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/CachePool.java index f2adb5e2..8f710491 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/CachePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/CachePool.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package com.arialyy.aria.core.queue.pool; import android.text.TextUtils; @@ -25,6 +24,7 @@ import com.arialyy.aria.core.task.Task; import java.util.HashMap; import java.util.Map; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; /** * Created by lyy on 2016/8/14. @@ -35,6 +35,7 @@ public class CachePool implements IPool { private static final Object LOCK = new Object(); private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数 private static volatile CachePool INSTANCE = null; + private static final long TIME_OUT = 1000; private Map mCacheArray; private LinkedBlockingQueue mCacheQueue; @@ -75,13 +76,19 @@ public class CachePool implements IPool { @Override public Task pollTask() { synchronized (LOCK) { - Task task = mCacheQueue.poll(); - if (task != null) { - String url = task.getDownloadEntity().getDownloadUrl(); - mCacheArray.remove(CommonUtil.keyToHashKey(url)); + try { + Task task = null; + task = mCacheQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS); + if (task != null) { + String url = task.getDownloadEntity().getDownloadUrl(); + mCacheArray.remove(CommonUtil.keyToHashKey(url)); + } + return task; + } catch (InterruptedException e) { + e.printStackTrace(); } - return task; } + return null; } @Override public Task getTask(String downloadUrl) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java index 6f3f5009..a3a1c911 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java @@ -137,12 +137,18 @@ public class ExecutePool implements IPool { @Override public Task pollTask() { synchronized (LOCK) { - Task task = mExecuteQueue.poll(); - if (task != null) { - String url = task.getDownloadEntity().getDownloadUrl(); - mExecuteArray.remove(CommonUtil.keyToHashKey(url)); + try { + Task task = null; + task = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS); + if (task != null) { + String url = task.getDownloadEntity().getDownloadUrl(); + mExecuteArray.remove(CommonUtil.keyToHashKey(url)); + } + return task; + } catch (InterruptedException e) { + e.printStackTrace(); } - return task; + return null; } }