修复同一时间调用多个start导致的任务重复问题 https://github.com/AriaLyy/Aria/issues/130

pull/330/head
AriaLyy 7 years ago
parent 2d64c50ed0
commit e68da0719c
  1. 3
      Aria/build.gradle
  2. 2
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  3. 5
      Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  4. 63
      Aria/src/main/java/com/arialyy/aria/core/queue/pool/BaseExecutePool.java
  5. 2
      AriaAnnotations/build.gradle
  6. 2
      AriaCompiler/build.gradle
  7. 2
      AriaFtpPlug/bintray-release.gradle
  8. 2
      AriaFtpPlug/build.gradle
  9. 38
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  10. 9
      app/src/main/res/values/strings.xml
  11. 4
      build.gradle
  12. 6
      gradle.properties

@ -24,7 +24,8 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:appcompat-v7:23.1.1'
compile project(':AriaAnnotations') compile project(':AriaAnnotations')
compile 'com.arialyy.aria:aria-ftp-plug:1.0.1' compile 'com.arialyy.aria:aria-ftp-plug:1.0.1'
// compile project(':AriaFtpPlug') // compile project(':AriaFtpPlug')
} }
apply from: 'bintray-release.gradle' //apply from: 'bintray-release.gradle'

@ -21,8 +21,8 @@ import android.util.Log;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.QueueMod; import com.arialyy.aria.core.common.QueueMod;
import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.util.NetUtils; import com.arialyy.aria.util.NetUtils;
/** /**

@ -85,6 +85,9 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
} }
@Override public void run() { @Override public void run() {
if (mConstance.isRunning) {
return;
}
startFlow(); startFlow();
} }
@ -92,11 +95,11 @@ public abstract class AbsFileer<ENTITY extends AbsNormalEntity, TASK_ENTITY exte
* 开始下载流程 * 开始下载流程
*/ */
private void startFlow() { private void startFlow() {
mConstance.resetState();
checkTask(); checkTask();
if (mListener instanceof IDownloadListener) { if (mListener instanceof IDownloadListener) {
((IDownloadListener) mListener).onPostPre(mEntity.getFileSize()); ((IDownloadListener) mListener).onPostPre(mEntity.getFileSize());
} }
mConstance.resetState();
if (!mTaskEntity.isSupportBP) { if (!mTaskEntity.isSupportBP) {
mThreadNum = 1; mThreadNum = 1;
mConstance.THREAD_NUM = mThreadNum; mConstance.THREAD_NUM = mThreadNum;

@ -88,16 +88,18 @@ public class BaseExecutePool<TASK extends AbsTask> implements IPool<TASK> {
* @param maxNum 下载数 * @param maxNum 下载数
*/ */
public void setMaxNum(int maxNum) { public void setMaxNum(int maxNum) {
try { synchronized (AriaManager.LOCK) {
ArrayBlockingQueue<TASK> temp = new ArrayBlockingQueue<>(maxNum); try {
TASK task; ArrayBlockingQueue<TASK> temp = new ArrayBlockingQueue<>(maxNum);
while ((task = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS)) != null) { TASK task;
temp.offer(task); while ((task = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS)) != null) {
temp.offer(task);
}
mExecuteQueue = temp;
mSize = maxNum;
} catch (InterruptedException e) {
e.printStackTrace();
} }
mExecuteQueue = temp;
mSize = maxNum;
} catch (InterruptedException e) {
e.printStackTrace();
} }
} }
@ -107,33 +109,37 @@ public class BaseExecutePool<TASK extends AbsTask> implements IPool<TASK> {
* @param newTask 新任务 * @param newTask 新任务
*/ */
boolean putNewTask(TASK newTask) { boolean putNewTask(TASK newTask) {
String url = newTask.getKey(); synchronized (AriaManager.LOCK) {
boolean s = mExecuteQueue.offer(newTask); String url = newTask.getKey();
Log.w(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + "】")); boolean s = mExecuteQueue.offer(newTask);
if (s) { Log.w(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + "】"));
mExecuteMap.put(CommonUtil.keyToHashKey(url), newTask); if (s) {
mExecuteMap.put(CommonUtil.keyToHashKey(url), newTask);
}
return s;
} }
return s;
} }
/** /**
* 队列满时将移除下载队列中的第一个任务 * 队列满时将移除下载队列中的第一个任务
*/ */
boolean pollFirstTask() { boolean pollFirstTask() {
try { synchronized (AriaManager.LOCK) {
TASK oldTask = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS); try {
if (oldTask == null) { TASK oldTask = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS);
Log.e(TAG, "移除任务失败"); if (oldTask == null) {
Log.e(TAG, "移除任务失败");
return false;
}
oldTask.stop();
String key = CommonUtil.keyToHashKey(oldTask.getKey());
mExecuteMap.remove(key);
} catch (InterruptedException e) {
e.printStackTrace();
return false; return false;
} }
oldTask.stop(); return true;
String key = CommonUtil.keyToHashKey(oldTask.getKey());
mExecuteMap.remove(key);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
} }
return true;
} }
@Override public TASK pollTask() { @Override public TASK pollTask() {
@ -183,7 +189,10 @@ public class BaseExecutePool<TASK extends AbsTask> implements IPool<TASK> {
} }
String convertKey = CommonUtil.keyToHashKey(key); String convertKey = CommonUtil.keyToHashKey(key);
TASK task = mExecuteMap.get(convertKey); TASK task = mExecuteMap.get(convertKey);
if (mExecuteQueue.remove(task)) { final int oldQueueSize = mExecuteQueue.size();
boolean isSuccess = mExecuteQueue.remove(task);
final int newQueueSize = mExecuteQueue.size();
if (isSuccess && newQueueSize != oldQueueSize) {
mExecuteMap.remove(convertKey); mExecuteMap.remove(convertKey);
return true; return true;
} }

@ -11,4 +11,4 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(dir: 'libs', include: ['*.jar'])
} }
apply from: 'bintray-release.gradle' //apply from: 'bintray-release.gradle'

@ -14,4 +14,4 @@ dependencies {
compile project(':AriaAnnotations') compile project(':AriaAnnotations')
} }
apply from: 'bintray-release.gradle' //apply from: 'bintray-release.gradle'

@ -3,7 +3,7 @@ publish {
artifactId = 'aria-ftp-plug' artifactId = 'aria-ftp-plug'
userOrg = rootProject.userOrg userOrg = rootProject.userOrg
groupId = rootProject.groupId groupId = rootProject.groupId
uploadName = 'AriaFtpPlug' uploadName = 'FtpPlug'
publishVersion = rootProject.publishVersion publishVersion = rootProject.publishVersion
description = rootProject.description description = rootProject.description
website = rootProject.website website = rootProject.website

@ -11,4 +11,4 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(dir: 'libs', include: ['*.jar'])
} }
//apply from: 'bintray-release.gradle' apply from: 'bintray-release.gradle'

@ -46,14 +46,14 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private static final String DOWNLOAD_URL = private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk"; "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4_1"; //"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4_1";
//"http://120.25.196.56:8000/filereq?id=15692406294&ipncid=105635&client=android&filename=20170819185541.avi"; //"http://120.25.196.56:8000/filereq?id=15692406294&ipncid=105635&client=android&filename=20170819185541.avi";
//"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt"; //"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt";
//"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815"; //"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815";
//"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"; //"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
//"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt"; //"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt";
"http://down8.androidgame-store.com/201706122321/97967927DD4E53D9905ECAA7874C8128/new/game1/19/45319/com.neuralprisma-2.5.2.174-2000174_1494784835.apk?f=web_1"; //"http://down8.androidgame-store.com/201706122321/97967927DD4E53D9905ECAA7874C8128/new/game1/19/45319/com.neuralprisma-2.5.2.174-2000174_1494784835.apk?f=web_1";
//不支持断点的链接 //不支持断点的链接
//"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-"; //"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
//"http://172.18.104.50:8080/download/_302turn"; //"http://172.18.104.50:8080/download/_302turn";
@ -72,8 +72,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
* 设置start stop 按钮状态 * 设置start stop 按钮状态
*/ */
private void setBtState(boolean state) { private void setBtState(boolean state) {
mStart.setEnabled(state); //mStart.setEnabled(state);
mStop.setEnabled(!state); //mStop.setEnabled(!state);
} }
@Override public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onCreateOptionsMenu(Menu menu) {
@ -195,11 +195,23 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public void onClick(View view) { public void onClick(View view) {
switch (view.getId()) { switch (view.getId()) {
case R.id.start: case R.id.start:
Aria.download(SingleTaskActivity.this) startD();
.load(DOWNLOAD_URL, true) startD();
.addHeader("groupName", "value") startD();
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/hhhhhhhh.apk") startD();
.start(); startD();
startD();
//new Thread(new Runnable() {
// @Override public void run() {
// startD();
// }
//}).start();
//
//new Thread(new Runnable() {
// @Override public void run() {
// startD();
// }
//}).start();
break; break;
case R.id.stop: case R.id.stop:
Aria.download(this).load(DOWNLOAD_URL).stop(); Aria.download(this).load(DOWNLOAD_URL).stop();
@ -209,4 +221,12 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
break; break;
} }
} }
private void startD(){
Aria.download(SingleTaskActivity.this)
.load(DOWNLOAD_URL, true)
.addHeader("groupName", "value")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/hhhhhhhh.apk")
.start();
}
} }

@ -24,6 +24,10 @@
<item>鲜果消消乐</item> <item>鲜果消消乐</item>
<item>航海奇迹</item> <item>航海奇迹</item>
<item>test_1</item>
<item>test_2</item>
<item>test_3</item>
</string-array> </string-array>
<string-array name="download_url"> <string-array name="download_url">
<item>https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk</item> <item>https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk</item>
@ -32,6 +36,11 @@
<item>http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk</item> <item>http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk</item>
<item>http://rs.0.gaoshouyou.com/d/e7/3d/73e716d3353de5b479fcf7da8d36a5ef.apk</item> <item>http://rs.0.gaoshouyou.com/d/e7/3d/73e716d3353de5b479fcf7da8d36a5ef.apk</item>
<item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item> <item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item>
<item>http://dby-resource-android.duobeiyun.com/jz22a8508b4265466b9fb4bb29082eaa2d.zip</item>
<item>http://dby-resource-android.duobeiyun.com/jz05fa8faf068145fcb25c93c8091297ad.zip</item>
<item>http://dby-resource-android.duobeiyun.com/jz684e1b4c2f6b4576979e60fd95edebad.zip</item>
</string-array> </string-array>
<string-array name="highest_names"> <string-array name="highest_names">

@ -37,8 +37,8 @@ task clean(type: Delete) {
ext { ext {
userOrg = 'arialyy' userOrg = 'arialyy'
groupId = 'com.arialyy.aria' groupId = 'com.arialyy.aria'
publishVersion = '3.3.3_dev' // publishVersion = '3.3.3_dev_3'
// publishVersion = '1.0.1' //FTP插件 publishVersion = '1.0.3' //FTP插件
repoName='maven' repoName='maven'
desc = 'android 下载框架' desc = 'android 下载框架'
website = 'https://github.com/AriaLyy/Aria' website = 'https://github.com/AriaLyy/Aria'

@ -16,6 +16,6 @@
#org.gradle.daemon=true #org.gradle.daemon=true
#org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 #org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
# gradle proxy https://chaosleong.github.io/2017/02/10/Configuring-Gradle-Proxy/ # gradle proxy https://chaosleong.github.io/2017/02/10/Configuring-Gradle-Proxy/
systemProp.socksProxyHost=127.0.0.1 #systemProp.socksProxyHost=127.0.0.1
systemProp.socksProxyPort=51110 #systemProp.socksProxyPort=60777
systemprop.socksProxyVersion=5 #systemprop.socksProxyVersion=5
Loading…
Cancel
Save