From 6657dbb155cc2bd656059de92c9c9e7b97b3facf Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Mon, 24 Jul 2017 16:40:20 +0800 Subject: [PATCH] fix bug https://github.com/AriaLyy/Aria/issues/80 --- .../java/com/arialyy/aria/core/inf/ITask.java | 28 +++++++++---------- .../arialyy/aria/core/queue/AbsTaskQueue.java | 17 +++++------ .../aria/core/queue/pool/BaseCachePool.java | 5 ++-- .../aria/core/queue/pool/BaseExecutePool.java | 3 +- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java index 70bee951..117fe6c0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java @@ -33,63 +33,63 @@ public interface ITask { /** * 唯一标识符,DownloadTask 为下载地址,UploadTask 为文件路径 */ - public String getKey(); + String getKey(); /** * 任务是否正在执行 * * @return true,正在执行; */ - public boolean isRunning(); + boolean isRunning(); /** * 获取信息实体 */ - public ENTITY getEntity(); + ENTITY getEntity(); - public void start(); + void start(); - public void stop(); + void stop(); - public void cancel(); + void cancel(); /** * 原始byte速度 */ - public long getSpeed(); + long getSpeed(); /** * 转换单位后的速度 */ - public String getConvertSpeed(); + String getConvertSpeed(); /** * 获取百分比进度 */ - public int getPercent(); + int getPercent(); /** * 原始文件byte长度 */ - public long getFileSize(); + long getFileSize(); /** * 转换单位后的文件长度 */ - public String getConvertFileSize(); + String getConvertFileSize(); /** * 获取当前进度 */ - public long getCurrentProgress(); + long getCurrentProgress(); /** * 获取单位转换后的进度 * * @return 返回 3mb */ - public String getConvertCurrentProgress(); + String getConvertCurrentProgress(); - public void setTargetName(String targetName); + void setTargetName(String targetName); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index 8dab1afb..acb59430 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -26,6 +26,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.queue.pool.BaseCachePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool; +import java.util.Iterator; import java.util.Set; /** @@ -52,14 +53,12 @@ abstract class AbsTaskQueue exeKeys = mExecutePool.getAllTask().keySet(); - for (String key : exeKeys) { + for (String key : mExecutePool.getAllTask().keySet()) { TASK task = mExecutePool.getAllTask().get(key); if (task != null) task.cancel(); } - Set cacheKeys = mCachePool.getAllTask().keySet(); - for (String key : cacheKeys) { - mExecutePool.removeTask(key); + for (String key : mCachePool.getAllTask().keySet()) { + mCachePool.removeTask(key); } } @@ -67,14 +66,12 @@ abstract class AbsTaskQueue exeKeys = mExecutePool.getAllTask().keySet(); - for (String key : exeKeys) { + for (String key : mExecutePool.getAllTask().keySet()) { TASK task = mExecutePool.getAllTask().get(key); if (task != null && task.isRunning()) task.stop(); } - Set cacheKeys = mCachePool.getAllTask().keySet(); - for (String key : cacheKeys) { - mExecutePool.removeTask(key); + for (String key : mCachePool.getAllTask().keySet()) { + mCachePool.removeTask(key); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java index 1dd9cd7c..200e72c9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; @@ -40,9 +41,9 @@ public class BaseCachePool implements IPool { private Map mCacheMap; private LinkedBlockingQueue mCacheQueue; - public BaseCachePool() { + BaseCachePool() { mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM); - mCacheMap = new HashMap<>(); + mCacheMap = new ConcurrentHashMap<>(); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java index ee9a10a0..b0bd0731 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java @@ -25,6 +25,7 @@ import com.arialyy.aria.util.CommonUtil; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; /** @@ -41,7 +42,7 @@ public class BaseExecutePool implements IPool { BaseExecutePool() { mSize = getMaxSize(); mExecuteQueue = new ArrayBlockingQueue<>(mSize); - mExecuteMap = new HashMap<>(); + mExecuteMap = new ConcurrentHashMap<>(); } /**