Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1b2619c

Browse files
committedDec 26, 2015
1. fix paused not so effect by large tasks queue(1. serial lock list change with serial message, 2. shutdown all wait thread by listener. 3. make sure event listener already added before notify paused; 2. fix download callback delay when queue large(split EventPool to 2 type(FileDownloadPool && FileDownloadProgressPool))
1 parent 8dedc6a commit 1b2619c

16 files changed

+309
-117
lines changed
 

‎library/src/main/java/com/liulishuo/filedownloader/BaseDownloadTask.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import android.text.TextUtils;
2020

21-
import com.liulishuo.filedownloader.event.DownloadEventPool;
21+
import com.liulishuo.filedownloader.event.FileDownloadEventPool;
2222
import com.liulishuo.filedownloader.event.IDownloadEvent;
2323
import com.liulishuo.filedownloader.event.IDownloadListener;
2424
import com.liulishuo.filedownloader.model.FileDownloadModel;
@@ -77,7 +77,7 @@ public abstract class BaseDownloadTask {
7777
// --------------------------------------- 以下 初始化 -----------------------------------------------
7878

7979
static {
80-
DownloadEventPool.getImpl().addListener(DownloadTaskEvent.ID, new IDownloadListener(Integer.MAX_VALUE) {
80+
FileDownloadEventPool.getImpl().addListener(DownloadTaskEvent.ID, new IDownloadListener(Integer.MAX_VALUE) {
8181
@Override
8282
public boolean callback(IDownloadEvent event) {
8383
final DownloadTaskEvent taskEvent = (DownloadTaskEvent) event;
@@ -207,7 +207,7 @@ public int start() {
207207

208208
if (ready) {
209209
// 在IPC的时候被block住等待Binder线程
210-
DownloadEventPool.getImpl().asyncPublishInNewThread(new DownloadTaskEvent(this)
210+
FileDownloadEventPool.getImpl().send2Service(new DownloadTaskEvent(this)
211211
.requestStart());
212212
}
213213

@@ -227,6 +227,8 @@ public boolean pause() {
227227

228228
final boolean result = _pauseExecute();
229229

230+
// For make sure already added event listener for receive paused event
231+
FileDownloadList.getImpl().add(this);
230232
if (result) {
231233
FileDownloadList.getImpl().removeByPaused(this);
232234
} else {
@@ -377,15 +379,15 @@ public int getRetryingTimes() {
377379
private void _addEventListener() {
378380
if (this.listener != null && !isAddedEventLst) {
379381
FileDownloadLog.d(this, "[_addEventListener] %s", generateEventId());
380-
DownloadEventPool.getImpl().addListener(generateEventId(), this.listener);
382+
FileDownloadEventPool.getImpl().addListener(generateEventId(), this.listener);
381383
isAddedEventLst = true;
382384
}
383385
}
384386

385387
private void _removeEventListener() {
386388
if (this.listener != null) {
387389
FileDownloadLog.d(this, "[_removeEventListener] %s", generateEventId());
388-
DownloadEventPool.getImpl().removeListener(generateEventId(), this.listener);
390+
FileDownloadEventPool.getImpl().removeListener(generateEventId(), this.listener);
389391
isAddedEventLst = false;
390392
}
391393
}

‎library/src/main/java/com/liulishuo/filedownloader/DownloadTaskEvent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* Created by Jacksgong on 12/23/15.
2323
*/
24-
class DownloadTaskEvent extends IDownloadEvent {
24+
public class DownloadTaskEvent extends IDownloadEvent {
2525

2626
public final static String ID = "event.download.task";
2727

@@ -41,6 +41,10 @@ public BaseDownloadTask consume() {
4141
return task;
4242
}
4343

44+
public FileDownloadListener getTaskListener() {
45+
return this.task == null ? null : this.task.getListener();
46+
}
47+
4448
private int operate;
4549

4650
public DownloadTaskEvent requestStart() {

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloadDriver.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.liulishuo.filedownloader;
1818

19-
import com.liulishuo.filedownloader.event.DownloadEventPool;
19+
import com.liulishuo.filedownloader.event.FileDownloadEventPool;
2020
import com.liulishuo.filedownloader.util.FileDownloadLog;
2121

2222
/**
@@ -43,7 +43,7 @@ public void notifyStarted() {
4343
public void notifyPending() {
4444
FileDownloadLog.d(this, "notify pending %s", download);
4545

46-
DownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
46+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
4747
.pending());
4848

4949
download.ing();
@@ -53,7 +53,7 @@ public void notifyPending() {
5353
public void notifyConnected() {
5454
FileDownloadLog.d(this, "notify connected %s", download);
5555

56-
DownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
56+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
5757
.connected());
5858

5959
download.ing();
@@ -68,7 +68,7 @@ public void notifyProgress() {
6868
return;
6969
}
7070

71-
DownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
71+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
7272
.progress());
7373

7474
download.ing();
@@ -81,7 +81,7 @@ public void notifyProgress() {
8181
public void notifyBlockComplete() {
8282
FileDownloadLog.d(this, "notify block completed %s %s", download, Thread.currentThread().getName());
8383

84-
DownloadEventPool.getImpl().publish(download.getIngEvent()
84+
FileDownloadEventPool.getImpl().publish(download.getIngEvent()
8585
.blockComplete());
8686
download.ing();
8787
}
@@ -90,7 +90,7 @@ public void notifyBlockComplete() {
9090
public void notifyRetry() {
9191
FileDownloadLog.d(this, "notify retry %s %d %d %s", download, download.getAutoRetryTimes(), download.getRetryingTimes(), download.getEx());
9292

93-
DownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
93+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getIngEvent()
9494
.retry());
9595

9696
download.ing();
@@ -100,7 +100,7 @@ public void notifyRetry() {
100100
@Override
101101
public void notifyWarn() {
102102
FileDownloadLog.d(this, "notify warn %s", download);
103-
DownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
103+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
104104
.warn());
105105

106106
download.over();
@@ -110,7 +110,7 @@ public void notifyWarn() {
110110
public void notifyError() {
111111
FileDownloadLog.e(this, download.getEx(), "notify error %s", download);
112112

113-
DownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
113+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
114114
.error());
115115

116116
download.over();
@@ -120,7 +120,7 @@ public void notifyError() {
120120
public void notifyPaused() {
121121
FileDownloadLog.d(this, "notify paused %s", download);
122122

123-
DownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
123+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
124124
.pause());
125125

126126
download.over();
@@ -130,7 +130,7 @@ public void notifyPaused() {
130130
public void notifyCompleted() {
131131
FileDownloadLog.d(this, "notify completed %s", download);
132132

133-
DownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
133+
FileDownloadEventPool.getImpl().asyncPublishInMain(download.getOverEvent()
134134
.complete());
135135

136136
download.over();

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloadServiceUIGuard.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@
1919
import android.os.IBinder;
2020
import android.os.RemoteException;
2121

22-
import com.liulishuo.filedownloader.event.DownloadEventPool;
2322
import com.liulishuo.filedownloader.event.DownloadTransferEvent;
23+
import com.liulishuo.filedownloader.event.FileDownloadEventPool;
2424
import com.liulishuo.filedownloader.i.IFileDownloadIPCCallback;
2525
import com.liulishuo.filedownloader.i.IFileDownloadIPCService;
26-
import com.liulishuo.filedownloader.model.FileDownloadStatus;
2726
import com.liulishuo.filedownloader.model.FileDownloadTransferModel;
2827
import com.liulishuo.filedownloader.services.BaseFileServiceUIGuard;
2928
import com.liulishuo.filedownloader.services.FileDownloadService;
@@ -70,13 +69,7 @@ public static class FileDownloadServiceCallback extends IFileDownloadIPCCallback
7069

7170
@Override
7271
public void callback(FileDownloadTransferModel transfer) throws RemoteException {
73-
if (transfer.getStatus() == FileDownloadStatus.completed) {
74-
// 中间有一个blockComplete未知耗时
75-
DownloadEventPool.getImpl().asyncPublishInNewThread(new DownloadTransferEvent(transfer));
76-
} else {
77-
// 发出去以后在BaseDownloadTask#update中进行转包到UI线程,基本不耗时
78-
DownloadEventPool.getImpl().asyncPublishInCelerityThread(new DownloadTransferEvent(transfer));
79-
}
72+
FileDownloadEventPool.getImpl().receiveByService(new DownloadTransferEvent(transfer));
8073
}
8174
}
8275

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloadTask.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.liulishuo.filedownloader;
1818

19-
import com.liulishuo.filedownloader.event.DownloadEventPool;
2019
import com.liulishuo.filedownloader.event.DownloadEventSampleListener;
2120
import com.liulishuo.filedownloader.event.DownloadServiceConnectChangedEvent;
2221
import com.liulishuo.filedownloader.event.DownloadTransferEvent;
22+
import com.liulishuo.filedownloader.event.FileDownloadEventPool;
2323
import com.liulishuo.filedownloader.event.IDownloadEvent;
2424
import com.liulishuo.filedownloader.model.FileDownloadTransferModel;
2525
import com.liulishuo.filedownloader.util.FileDownloadHelper;
@@ -38,8 +38,8 @@ class FileDownloadTask extends BaseDownloadTask {
3838

3939
static {
4040
DOWNLOAD_INTERNAL_LIS = new DownloadEventSampleListener(new FileDownloadInternalLis());
41-
DownloadEventPool.getImpl().addListener(DownloadServiceConnectChangedEvent.ID, DOWNLOAD_INTERNAL_LIS);
42-
DownloadEventPool.getImpl().addListener(DownloadTransferEvent.ID, DOWNLOAD_INTERNAL_LIS);
41+
FileDownloadEventPool.getImpl().addListener(DownloadServiceConnectChangedEvent.ID, DOWNLOAD_INTERNAL_LIS);
42+
FileDownloadEventPool.getImpl().addListener(DownloadTransferEvent.ID, DOWNLOAD_INTERNAL_LIS);
4343
}
4444

4545
FileDownloadTask(String url) {

‎library/src/main/java/com/liulishuo/filedownloader/FileDownloader.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import android.os.HandlerThread;
2222
import android.os.Message;
2323

24-
import com.liulishuo.filedownloader.event.DownloadEventPool;
25-
import com.liulishuo.filedownloader.event.DownloadEventPoolImpl;
24+
import com.liulishuo.filedownloader.event.FileDownloadEventPool;
2625
import com.liulishuo.filedownloader.util.FileDownloadHelper;
2726
import com.liulishuo.filedownloader.util.FileDownloadLog;
2827

@@ -45,7 +44,6 @@ public static void init(final Application application) {
4544
// 下载进程与非下载进程都存一个
4645
FileDownloadLog.d(FileDownloader.class, "init Downloader");
4746
FileDownloadHelper.initAppContext(application);
48-
DownloadEventPool.setImpl(new DownloadEventPoolImpl());
4947
}
5048

5149
private final static class HolderClass {
@@ -102,20 +100,28 @@ public List<Integer> start(final FileDownloadListener listener, final boolean is
102100
* @see #pause(int)
103101
*/
104102
public void pause(final FileDownloadListener listener) {
105-
final BaseDownloadTask[] downloadList = FileDownloadList.getImpl().copy();
106-
for (BaseDownloadTask baseDownloadTask : downloadList) {
107-
if (baseDownloadTask.getListener() == listener) {
103+
FileDownloadEventPool.getImpl().shutdownSendPool(listener);
104+
final List<BaseDownloadTask> downloadList = FileDownloadList.getImpl().copy(listener);
105+
synchronized (pauseLock) {
106+
for (BaseDownloadTask baseDownloadTask : downloadList) {
108107
baseDownloadTask.pause();
109108
}
110109
}
111110

111+
112112
}
113113

114+
private final static Object pauseLock = new Object();
115+
114116
public void pauseAll() {
117+
FileDownloadEventPool.getImpl().shutdownSendPool();
115118
final BaseDownloadTask[] downloadList = FileDownloadList.getImpl().copy();
116-
for (BaseDownloadTask baseDownloadTask : downloadList) {
117-
baseDownloadTask.pause();
119+
synchronized (pauseLock) {
120+
for (BaseDownloadTask baseDownloadTask : downloadList) {
121+
baseDownloadTask.pause();
122+
}
118123
}
124+
119125
}
120126

121127
/**
@@ -215,13 +221,16 @@ public boolean handleMessage(final Message msg) {
215221
}
216222

217223
final BaseDownloadTask task = this.list.get(msg.arg1);
218-
if (!FileDownloadList.getImpl().contains(task)) {
219-
// pause?
220-
FileDownloadLog.d(SerialHandlerCallback.class, "direct go next by not contains %s %d", task, msg.arg1);
221-
goNext(msg.arg1 + 1);
222-
return true;
224+
synchronized (pauseLock) {
225+
if (!FileDownloadList.getImpl().contains(task)) {
226+
// pause?
227+
FileDownloadLog.d(SerialHandlerCallback.class, "direct go next by not contains %s %d", task, msg.arg1);
228+
goNext(msg.arg1 + 1);
229+
return true;
230+
}
223231
}
224232

233+
225234
list.get(msg.arg1)
226235
.setFinishListener(new BaseDownloadTask.FinishListener() {
227236
private int index;

‎library/src/main/java/com/liulishuo/filedownloader/event/DownloadEventPool.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

‎library/src/main/java/com/liulishuo/filedownloader/event/DownloadEventPoolImpl.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,19 @@
2828
import java.util.HashMap;
2929
import java.util.LinkedList;
3030
import java.util.concurrent.ExecutorService;
31-
import java.util.concurrent.Executors;
3231
import java.util.concurrent.LinkedBlockingQueue;
3332
import java.util.concurrent.ThreadPoolExecutor;
3433
import java.util.concurrent.TimeUnit;
3534

3635
/**
3736
* Created by Jacksgong on 15/6/23.
3837
*/
39-
final public class DownloadEventPoolImpl implements IDownloadEventPool {
38+
public class DownloadEventPoolImpl implements IDownloadEventPool {
4039

41-
private final ExecutorService threadPool = new ThreadPoolExecutor(3, 6,
42-
0L, TimeUnit.MILLISECONDS,
40+
private final ExecutorService threadPool = new ThreadPoolExecutor(3, 30,
41+
10, TimeUnit.SECONDS,
4342
new LinkedBlockingQueue<Runnable>());
4443

45-
// 处理一些敏捷快速的事件,通常耗时在1s以内,
46-
private final ExecutorService celerityThreadPool = Executors.newFixedThreadPool(2);
47-
4844
private final HashMap<String, LinkedList<IDownloadListener>> listenersMap = new HashMap<>();
4945

5046
private final Handler handler;
@@ -122,17 +118,6 @@ public void run() {
122118
});
123119
}
124120

125-
@Override
126-
public void asyncPublishInCelerityThread(final IDownloadEvent event) {
127-
128-
celerityThreadPool.execute(new Runnable() {
129-
@Override
130-
public void run() {
131-
DownloadEventPoolImpl.this.publish(event);
132-
}
133-
});
134-
}
135-
136121
@Override
137122
public void asyncPublishInMain(final IDownloadEvent event) {
138123
handler.post(new Runnable() {
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.