AriaLyy 7 years ago
parent 4f1412b554
commit 6657dbb155
  1. 28
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  2. 17
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  3. 5
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseCachePool.java
  4. 3
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java

@ -33,63 +33,63 @@ public interface ITask<ENTITY extends AbsEntity> {
/** /**
* 唯一标识符DownloadTask 为下载地址UploadTask 为文件路径 * 唯一标识符DownloadTask 为下载地址UploadTask 为文件路径
*/ */
public String getKey(); String getKey();
/** /**
* 任务是否正在执行 * 任务是否正在执行
* *
* @return true正在执行 * @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速度 * 原始byte速度
*/ */
public long getSpeed(); long getSpeed();
/** /**
* 转换单位后的速度 * 转换单位后的速度
*/ */
public String getConvertSpeed(); String getConvertSpeed();
/** /**
* 获取百分比进度 * 获取百分比进度
*/ */
public int getPercent(); int getPercent();
/** /**
* 原始文件byte长度 * 原始文件byte长度
*/ */
public long getFileSize(); long getFileSize();
/** /**
* 转换单位后的文件长度 * 转换单位后的文件长度
*/ */
public String getConvertFileSize(); String getConvertFileSize();
/** /**
* 获取当前进度 * 获取当前进度
*/ */
public long getCurrentProgress(); long getCurrentProgress();
/** /**
* 获取单位转换后的进度 * 获取单位转换后的进度
* *
* @return 返回 3mb * @return 返回 3mb
*/ */
public String getConvertCurrentProgress(); String getConvertCurrentProgress();
public void setTargetName(String targetName); void setTargetName(String targetName);
} }

@ -26,6 +26,7 @@ import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.queue.pool.BaseCachePool; import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
/** /**
@ -52,14 +53,12 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
} }
@Override public void removeAllTask() { @Override public void removeAllTask() {
Set<String> exeKeys = mExecutePool.getAllTask().keySet(); for (String key : mExecutePool.getAllTask().keySet()) {
for (String key : exeKeys) {
TASK task = mExecutePool.getAllTask().get(key); TASK task = mExecutePool.getAllTask().get(key);
if (task != null) task.cancel(); if (task != null) task.cancel();
} }
Set<String> cacheKeys = mCachePool.getAllTask().keySet(); for (String key : mCachePool.getAllTask().keySet()) {
for (String key : cacheKeys) { mCachePool.removeTask(key);
mExecutePool.removeTask(key);
} }
} }
@ -67,14 +66,12 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
* 停止所有任务 * 停止所有任务
*/ */
@Override public void stopAllTask() { @Override public void stopAllTask() {
Set<String> exeKeys = mExecutePool.getAllTask().keySet(); for (String key : mExecutePool.getAllTask().keySet()) {
for (String key : exeKeys) {
TASK task = mExecutePool.getAllTask().get(key); TASK task = mExecutePool.getAllTask().get(key);
if (task != null && task.isRunning()) task.stop(); if (task != null && task.isRunning()) task.stop();
} }
Set<String> cacheKeys = mCachePool.getAllTask().keySet(); for (String key : mCachePool.getAllTask().keySet()) {
for (String key : cacheKeys) { mCachePool.removeTask(key);
mExecutePool.removeTask(key);
} }
} }

@ -26,6 +26,7 @@ import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -40,9 +41,9 @@ public class BaseCachePool<TASK extends AbsTask> implements IPool<TASK> {
private Map<String, TASK> mCacheMap; private Map<String, TASK> mCacheMap;
private LinkedBlockingQueue<TASK> mCacheQueue; private LinkedBlockingQueue<TASK> mCacheQueue;
public BaseCachePool() { BaseCachePool() {
mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM); mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM);
mCacheMap = new HashMap<>(); mCacheMap = new ConcurrentHashMap<>();
} }
/** /**

@ -25,6 +25,7 @@ import com.arialyy.aria.util.CommonUtil;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -41,7 +42,7 @@ public class BaseExecutePool<TASK extends AbsTask> implements IPool<TASK> {
BaseExecutePool() { BaseExecutePool() {
mSize = getMaxSize(); mSize = getMaxSize();
mExecuteQueue = new ArrayBlockingQueue<>(mSize); mExecuteQueue = new ArrayBlockingQueue<>(mSize);
mExecuteMap = new HashMap<>(); mExecuteMap = new ConcurrentHashMap<>();
} }
/** /**

Loading…
Cancel
Save