fix: 调整代码,剩余时间要重新计算

This commit is contained in:
kyle
2022-03-19 10:46:53 +08:00
parent bf72136ab9
commit 3b357d7140
2 changed files with 7 additions and 4 deletions

View File

@@ -364,7 +364,7 @@ public abstract class WorkerWrapper<T, V> {
case TAKE_REST:
//FIXME 等待200毫秒重新投入线程池主要为了调起最后一个任务
Thread.sleep(200L);
executorService.submit(() -> this.work(executorService, fromWrapper, remainTime, group));
executorService.submit(() -> this.work(executorService, fromWrapper, remainTime-(SystemClock.now()-now), group));
return;
case FAST_FAIL:
if (setState(state, STARTED, ERROR)) {

View File

@@ -1,6 +1,7 @@
package v15.cases;
import com.jd.platform.async.executor.Async;
import com.jd.platform.async.executor.timer.SystemClock;
import com.jd.platform.async.wrapper.WorkerWrapper;
import com.jd.platform.async.wrapper.WorkerWrapperBuilder;
@@ -18,8 +19,8 @@ class Case1 {
try {
if ("F".equals(id)) {
System.out.println("wrapper(id=" + id + ") is working");
Thread.sleep(5000);
}else {
Thread.sleep(12000);
} else {
System.out.println("wrapper(id=" + id + ") is worki444ng");
}
} catch (InterruptedException e) {
@@ -30,6 +31,7 @@ class Case1 {
}
public static void main(String[] args) {
long now = SystemClock.now();
WorkerWrapper<?, ?> a = builder("A").build();
WorkerWrapper<?, ?> d;
builder("H")
@@ -46,10 +48,11 @@ class Case1 {
)
.build();
try {
Async.work(1000, a, d).awaitFinish();
Async.work(10000, a, d).awaitFinish();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("now:" + (SystemClock.now() - now));
/* 输出:
wrapper(id=D) is working
wrapper(id=A) is working