This commit is contained in:
lyy
2016-10-08 15:11:16 +08:00
parent 9c2015c90b
commit 0d9afbe1b4
8 changed files with 63 additions and 41 deletions

View File

@ -100,9 +100,10 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
/**
* 获取任务执行池
*
* @return
*/
public ExecutePool getExecutePool(){
public ExecutePool getExecutePool() {
return mExecutePool;
}
@ -152,11 +153,7 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
}
switch (msg.what) {
case STOP:
startNextTask(entity);
break;
case CANCEL:
startNextTask(entity);
break;
case COMPLETE:
startNextTask(entity);
break;
@ -187,7 +184,7 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
target.mTargetListener.onTaskCancel(task);
break;
case COMPLETE:
target.mTargetListener.onTaskCancel(task);
target.mTargetListener.onTaskComplete(task);
break;
case FAIL:
target.mTargetListener.onTaskFail(task);
@ -222,7 +219,9 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
Log.w(TAG, "没有下一任务");
return;
}
target.startTask(newTask);
if (newTask.getDownloadEntity().getState() == DownloadEntity.STATE_WAIT) {
target.startTask(newTask);
}
}
}
}

View File

@ -3,25 +3,25 @@ package com.arialyy.downloadutil.core.pool;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.downloadutil.util.Task;
import com.arialyy.downloadutil.core.inf.IPool;
import com.arialyy.downloadutil.util.Task;
import com.arialyy.downloadutil.util.Util;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Created by lyy on 2016/8/14.
* 任务缓存池,所有下载任务最先缓存在这个池中
*/
public class CachePool implements IPool {
private static final String TAG = "CachePool";
private static final Object LOCK = new Object();
private static volatile CachePool INSTANCE = null;
private Map<String, Task> mCacheArray;
private Queue<Task> mCacheQueue;
private static final String TAG = "CachePool";
private static final Object LOCK = new Object();
private static final int MAX_NUM = Integer.MAX_VALUE; //最大下载任务数
private static volatile CachePool INSTANCE = null;
private Map<String, Task> mCacheArray;
private LinkedBlockingQueue<Task> mCacheQueue;
public static CachePool getInstance() {
if (INSTANCE == null) {
@ -33,7 +33,7 @@ public class CachePool implements IPool {
}
private CachePool() {
mCacheQueue = new PriorityQueue<>();
mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM);
mCacheArray = new HashMap<>();
}
@ -48,7 +48,7 @@ public class CachePool implements IPool {
Log.w(TAG, "队列中已经包含了该任务,任务下载链接【" + url + "");
return false;
} else {
boolean s = mCacheQueue.offer(task);
boolean s = mCacheQueue.offer(task);
Log.d(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + ""));
if (s) {
mCacheArray.put(Util.keyToHashKey(url), task);

View File

@ -89,7 +89,7 @@ public class ExecutePool implements IPool {
return false;
}
oldTask.stop();
wait(200);
// wait(200);
String key = Util.keyToHashKey(oldTask.getDownloadEntity().getDownloadUrl());
mExecuteArray.remove(key);
} catch (InterruptedException e) {

View File

@ -59,7 +59,7 @@ public class DbEntity {
/**
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
*/
public synchronized void save() {
public void save() {
if (mUtil.tableExists(getClass()) && thisIsExist()) {
update();
} else {

View File

@ -62,7 +62,7 @@ public class DbUtil {
/**
* 删除某条数据
*/
<T extends DbEntity> void delData(Class<T> clazz, @NonNull Object[] wheres,
synchronized <T extends DbEntity> void delData(Class<T> clazz, @NonNull Object[] wheres,
@NonNull Object[] values) {
mDb = mHelper.getWritableDatabase();
if (wheres.length <= 0 || values.length <= 0) {
@ -88,7 +88,7 @@ public class DbUtil {
/**
* 修改某行数据
*/
void modifyData(DbEntity dbEntity) {
synchronized void modifyData(DbEntity dbEntity) {
mDb = mHelper.getWritableDatabase();
Class<?> clazz = dbEntity.getClass();
Field[] fields = Util.getFields(clazz);
@ -123,7 +123,7 @@ public class DbUtil {
/**
* 遍历所有数据
*/
<T extends DbEntity> List<T> findAllData(Class<T> clazz) {
synchronized <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
if (!tableExists(clazz)) {
createTable(clazz);
}
@ -138,7 +138,7 @@ public class DbUtil {
/**
* 条件查寻数据
*/
<T extends DbEntity> List<T> findData(Class<T> clazz, @NonNull String[] wheres,
synchronized <T extends DbEntity> List<T> findData(Class<T> clazz, @NonNull String[] wheres,
@NonNull String[] values) {
if (!tableExists(clazz)) {
createTable(clazz);
@ -167,7 +167,7 @@ public class DbUtil {
/**
* 插入数据
*/
void insertData(DbEntity dbEntity) {
synchronized void insertData(DbEntity dbEntity) {
Class<?> clazz = dbEntity.getClass();
if (!tableExists(clazz)) {
createTable(clazz);
@ -245,7 +245,7 @@ public class DbUtil {
/**
* 创建表
*/
private void createTable(Class clazz) {
private synchronized void createTable(Class clazz) {
if (mDb == null || !mDb.isOpen()) {
mDb = mHelper.getWritableDatabase();
}
@ -321,7 +321,7 @@ public class DbUtil {
/**
* 关闭数据库
*/
private void close() {
private synchronized void close() {
if (mDb != null) {
mDb.close();
}
@ -330,7 +330,7 @@ public class DbUtil {
/**
* 获取所在行Id
*/
int[] getRowId(Class clazz) {
synchronized int[] getRowId(Class clazz) {
mDb = mHelper.getReadableDatabase();
Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(clazz), null);
int[] ids = new int[cursor.getCount()];
@ -347,7 +347,7 @@ public class DbUtil {
/**
* 获取行Id
*/
int getRowId(Class clazz, Object[] wheres, Object[] values) {
synchronized int getRowId(Class clazz, Object[] wheres, Object[] values) {
mDb = mHelper.getReadableDatabase();
if (wheres.length <= 0 || values.length <= 0) {
Log.e(TAG, "请输入删除条件");
@ -375,7 +375,7 @@ public class DbUtil {
/**
* 根据数据游标创建一个具体的对象
*/
private <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz, Cursor cursor) {
private synchronized <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz, Cursor cursor) {
Field[] fields = Util.getFields(clazz);
List<T> entitys = new ArrayList<>();
if (fields != null && fields.length > 0) {

View File

@ -65,7 +65,7 @@ public class Util {
*/
public static int keyToHashCode(String str) {
int total = 0;
for (int i = 0; i < str.length() && i < 6; i++) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == '-') ch = (char) 28; // does not contain the same last 5 bits as any letter
if (ch == '\'') ch = (char) 29; // nor this