Skip to content

Commit

Permalink
feat: 下载任务的优先级考虑失败次数
Browse files Browse the repository at this point in the history
  • Loading branch information
nICEnnnnnnnLee committed Jun 28, 2024
1 parent c913263 commit b82bc1b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/nicelee/ui/item/DownloadInfoPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ public void continueTask() {
if (status != StatusEnum.DOWNLOADING && status != StatusEnum.SUCCESS && status != StatusEnum.PROCESSING) {
downloader.startTask();
if(!Global.downLoadThreadPool.isShutdown()){
Global.downLoadThreadPool.execute(new DownloadRunnableInternal(this, System.currentTimeMillis(), true));
Global.downLoadThreadPool.execute(
new DownloadRunnableInternal(this, System.currentTimeMillis(), true, failCnt));
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/nicelee/ui/thread/DownloadExecutors.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ public class DownloadExecutors {
@Override
public int compare(DownloadRunnableInternal o1, DownloadRunnableInternal o2) {
// o1.invokeByContinueTask 为true时优先级更高
// o1.failCnt 越大优先级更高(为0时特殊考虑)
// o1.urlTimestamp 越小优先级更高
if (o1.invokeByContinueTask == o2.invokeByContinueTask) {
return (int) (o1.urlTimestamp - o2.urlTimestamp);
if (o1.failCnt == o2.failCnt) {
return (int) (o1.urlTimestamp - o2.urlTimestamp);
} else {
// 走到这个分支不可能有“开始下载”的任务,failCnt == 0表示人为重新开始任务
if(o1.failCnt == 0)
return -1;
if(o2.failCnt == 0)
return -1;
return o2.failCnt - o1.failCnt;
}
} else {
return o1.invokeByContinueTask ? -1 : 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/nicelee/ui/thread/DownloadRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void download() {
jpContent.add(downPanel);
jpContent.setPreferredSize(new Dimension(1100, 128 * Global.downloadTaskList.size()));
if(!Global.downLoadThreadPool.isShutdown()){
Global.downLoadThreadPool.execute(new DownloadRunnableInternal(downPanel, System.currentTimeMillis(), false));
Global.downLoadThreadPool.execute(new DownloadRunnableInternal(downPanel, System.currentTimeMillis(), false, 0));
}
if(Global.sleepAfterDownloadQuery > 0) {
try {
Expand Down
5 changes: 4 additions & 1 deletion src/nicelee/ui/thread/DownloadRunnableInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DownloadRunnableInternal implements Runnable {
final DownloadInfoPanel downPanel;
final long urlTimestamp;
final boolean invokeByContinueTask;
final int failCnt;
// 下面这些值都可以从 downPanel 直接或者间接获取
final String record;
final INeedAV iNeedAV;
Expand All @@ -30,11 +31,13 @@ public class DownloadRunnableInternal implements Runnable {
* @param downPanel 下载任务绑定的UI控件,相关信息可以从这里获取
* @param urlTimestamp 当前下载链接的获取时间
* @param invokeByContinueTask 该线程是否是 “继续任务”, 而不是 “开始任务”
* @param failCnt 在此之前的任务失败次数
*/
public DownloadRunnableInternal(DownloadInfoPanel downPanel, long urlTimestamp, boolean invokeByContinueTask) {
public DownloadRunnableInternal(DownloadInfoPanel downPanel, long urlTimestamp, boolean invokeByContinueTask, int failCnt) {
this.downPanel = downPanel;
this.urlTimestamp = urlTimestamp;
this.invokeByContinueTask = invokeByContinueTask;
this.failCnt = failCnt;
iNeedAV = downPanel.iNeedAV;
downloader = iNeedAV.getDownloader();
urlQuery = downPanel.url;
Expand Down

0 comments on commit b82bc1b

Please sign in to comment.