修改多个依赖时,极端情况下,会发生同时进入doDependsJobs方法

This commit is contained in:
wuweifeng10 2020-01-08 17:32:38 +08:00
parent 4387a0d63a
commit d666a3e620
3 changed files with 38 additions and 4 deletions

View File

@ -90,7 +90,7 @@ public class WorkerWrapper<T, V> {
}
//如果自己已经执行过了
//可能有多个依赖其中的一个依赖已经执行完了并且自己也已开始执行或执行完毕当另一个依赖执行完毕又进来该方法时就不重复处理了
if (getState() != INIT) {
if (getState() == FINISH || getState() == ERROR) {
beginNext(poolExecutor, now, remainTime);
return;
}
@ -172,7 +172,7 @@ public class WorkerWrapper<T, V> {
}
}
private void doDependsJobs(ThreadPoolExecutor poolExecutor, List<DependWrapper> dependWrappers, WorkerWrapper fromWrapper, long now, long remainTime) {
private synchronized void doDependsJobs(ThreadPoolExecutor poolExecutor, List<DependWrapper> dependWrappers, WorkerWrapper fromWrapper, long now, long remainTime) {
boolean nowDependIsMust = false;
//创建必须完成的上游wrapper集合
Set<DependWrapper> mustWrapper = new HashSet<>();
@ -232,6 +232,7 @@ public class WorkerWrapper<T, V> {
return;
}
System.out.println(Thread.currentThread().getName()+"---" + existNoFinish);
//如果上游都没有失败分为两种情况一种是都finish了一种是有的在working
//都finish的话
if (!existNoFinish) {

View File

@ -10,11 +10,16 @@ import com.tianyalei.async.worker.WorkResult;
* @author wuweifeng wrote on 2019-11-20.
*/
public class ParWorker1 implements IWorker<String, String>, ICallback<String, String> {
private long sleepTime = 1000;
public void setSleepTime(long sleepTime) {
this.sleepTime = sleepTime;
}
@Override
public String action(String object) {
try {
Thread.sleep(1000);
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}

View File

@ -27,7 +27,8 @@ public class TestPar {
// testMulti4();
// testMulti5();
// testMulti6();
testMulti7();
// testMulti7();
testMulti8();
}
/**
@ -352,4 +353,31 @@ public class TestPar {
System.out.println(getThreadCount());
Async.shutDown();
}
/**
* a1 -> b -> c
* a2 -> b -> c
*/
private static void testMulti8() throws ExecutionException, InterruptedException {
ParWorker w = new ParWorker();
ParWorker1 w1 = new ParWorker1();
// w1.setSleepTime(1005);
ParWorker2 w2 = new ParWorker2();
w2.setSleepTime(3000);
ParWorker3 w3 = new ParWorker3();
w3.setSleepTime(1000);
WorkerWrapper<String, String> workerWrapper = new WorkerWrapper<>(w, "a1", w);
WorkerWrapper<String, String> workerWrapper1 = new WorkerWrapper<>(w1, "a2", w1);
WorkerWrapper<String, String> workerWrapper2 = new WorkerWrapper<>(w2, "b", w2);
WorkerWrapper<String, String> workerWrapper3 = new WorkerWrapper<>(w3, "c", w3);
workerWrapper.addNext(workerWrapper2);
workerWrapper1.addNext(workerWrapper2);
workerWrapper2.addNext(workerWrapper3);
Async.beginWork(6000, workerWrapper, workerWrapper1);
Async.shutDown();
}
}