Skip to content

Commit 905f3be

Browse files
committed
增强超时控制:永不超时
1 parent 3334e56 commit 905f3be

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

AndroidExecLibrary/src/main/java/com/excellence/exec/Command.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class Command {
2020

2121
private static final String TAG = Command.class.getSimpleName();
2222

23-
protected static final int DEFAULT_TIME_OUT = 10 * 1000;
24-
2523
private final LinkedList<CommandTask> mTaskQueue;
2624
private int mParallelTaskCount = 0;
2725
private long mTimeOut = 0;
@@ -34,9 +32,6 @@ protected Command(CommanderOptions options) {
3432
mParallelTaskCount = Integer.MAX_VALUE;
3533
}
3634
mTimeOut = options.mTimeOut;
37-
if (mTimeOut <= 0) {
38-
mTimeOut = DEFAULT_TIME_OUT;
39-
}
4035

4136
final Handler handler = new Handler(Looper.getMainLooper());
4237
mResponsePoster = new Executor() {

AndroidExecLibrary/src/main/java/com/excellence/exec/CommandTask.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ private CommandTask(Builder builder) {
4949

5050
mCommand = builder.mCommand;
5151
mTimeUnit = builder.mTimeUnit;
52-
mTimeOut = builder.mTimeOut;
53-
if (mTimeOut <= 0) {
54-
mTimeOut = mManager.getTimeOut();
52+
/**
53+
* 单任务超时配置覆盖全局超时配置
54+
*/
55+
mTimeOut = mManager.getTimeOut();
56+
if (builder.isOverrideTimeOut) {
57+
mTimeOut = builder.mTimeOut;
5558
}
5659
mTimeDelay = builder.mTimeDelay;
5760
if (mTimeDelay < 0) {
@@ -67,6 +70,7 @@ public static class Builder {
6770
private TimeUnit mTimeUnit = TimeUnit.MILLISECONDS;
6871
private long mTimeOut = 0;
6972
private long mTimeDelay = 0;
73+
private boolean isOverrideTimeOut = false;
7074

7175
/**
7276
* 单个命令参数
@@ -123,13 +127,14 @@ public Builder timeUnit(TimeUnit timeUnit) {
123127
}
124128

125129
/**
126-
* 单独设置任务超时时间,默认:10 * 1000ms {@link Command#DEFAULT_TIME_OUT}
130+
* 单独设置任务超时时间
127131
*
128132
* @param timeOut
129133
* @return
130134
*/
131135
public Builder timeOut(long timeOut) {
132136
mTimeOut = timeOut;
137+
isOverrideTimeOut = true;
133138
return this;
134139
}
135140

@@ -259,6 +264,11 @@ private void resetTimer() {
259264

260265
private void restartTimer() {
261266
resetTimer();
267+
mTimerTask = null;
268+
if (mTimeOut <= 0) {
269+
return;
270+
}
271+
262272
mTimerTask = Observable.timer(mTimeOut, mTimeUnit).subscribe(new Consumer<Long>() {
263273
@Override
264274
public void accept(Long aLong) throws Exception {

AndroidExecLibrary/src/main/java/com/excellence/exec/Commander.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import java.util.Arrays;
77
import java.util.List;
88

9-
import static com.excellence.exec.Command.DEFAULT_TIME_OUT;
10-
119
/**
1210
* <pre>
1311
* author : VeiZhang
@@ -20,12 +18,14 @@ public class Commander {
2018

2119
private static final String TAG = Commander.class.getSimpleName();
2220

21+
private static final int DEFAULT_TIME_OUT = 10 * 1000;
22+
2323
private static Commander mInstance = null;
2424

2525
private Command mCommand = null;
2626

2727
/**
28-
* 默认:不限制并发线程数;指令超时10s终止
28+
* 默认:不限制并发线程数;指令超时{@link Commander#DEFAULT_TIME_OUT}终止
2929
*/
3030
public static void init() {
3131
init(new CommanderOptions.Builder().parallelTaskCount(Integer.MAX_VALUE).timeOut(DEFAULT_TIME_OUT).build());

AndroidExecLibrary/src/main/java/com/excellence/exec/CommanderOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public Builder parallelTaskCount(int parallelTaskCount) {
3434

3535
/**
3636
*
37-
* @param timeOut ms:执行命令超时时间(存在某些命令会需要人为输入确认,此时命令会一直卡住等待),默认10s超时,终止指令
37+
* @param timeOut ms:执行命令超时时间(存在某些命令会需要人为输入确认,此时命令会一直卡住等待),终止指令
3838
*/
3939
public Builder timeOut(long timeOut) {
4040
mTimeOut = timeOut;

0 commit comments

Comments
 (0)