refactor: 将改变值的函数抽取到抽象类

This commit is contained in:
kyle
2022-07-04 19:58:32 +08:00
parent 5af4c07852
commit fb7e3419cc
3 changed files with 22 additions and 8 deletions

View File

@@ -7,6 +7,9 @@ import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;
import com.jd.platform.async.wrapper.WorkerWrapperBuilder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 示例:简单示例--复杂点的
*
@@ -27,6 +30,7 @@ class Case1 {
}
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException("被中断了");
}
return id;
}).callback((new ICallback<String, String>() {
@@ -37,6 +41,9 @@ class Case1 {
@Override
public void result(boolean success, String param, WorkResult<String> workResult) {
// if ("H".equals(id)) {
// int a=1/0;
// }
System.out.println("\t\twrapper(id=" + id + ") callback "
+ (success ? "success " : "fail ")
+ ", workResult is " + workResult);
@@ -45,7 +52,8 @@ class Case1 {
.allowInterrupt(true);
}
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
final ExecutorService executorService = Executors.newSingleThreadExecutor();
long now = SystemClock.now();
WorkerWrapper<?, ?> a = builder("A").build();
WorkerWrapper<?, ?> d = builder("D").build();
@@ -63,10 +71,11 @@ class Case1 {
)
.build();
try {
Async.work(1000, a, d).awaitFinish();
Async.work(1000, executorService, a, d).awaitFinish();
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.shutdown();
System.out.println("now:" + (SystemClock.now() - now));
/* 输出:
wrapper(id=D) is working

View File

@@ -1,5 +1,7 @@
package com.jd.platform.async.openutil.timer;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author create by TcSnZh on 2021/5/12-下午6:36
*/
@@ -8,6 +10,8 @@ public abstract class AbstractWheelTimer implements Timer, AutoCloseable {
public static final int WORKER_STATE_STARTED = 1;
public static final int WORKER_STATE_SHUTDOWN = 2;
protected final AtomicInteger workerState = new AtomicInteger(WORKER_STATE_INIT); // 0 - init, 1 - started, 2 - shut down
public abstract void start();
@SuppressWarnings("RedundantThrows")
@@ -15,4 +19,9 @@ public abstract class AbstractWheelTimer implements Timer, AutoCloseable {
public void close() throws Exception {
stop();
}
protected boolean changeState(int workerStateStarted, int workerStateShutdown) {
return workerState.compareAndSet(workerStateStarted, workerStateShutdown);
}
}

View File

@@ -2,7 +2,6 @@ package com.jd.platform.async.openutil.timer;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLong;
@@ -22,8 +21,6 @@ public class HashedWheelTimer extends AbstractWheelTimer {
private final Worker worker = new Worker();
private final Thread workerThread;
@SuppressWarnings({"unused", "FieldMayBeFinal"})
private final AtomicInteger workerState = new AtomicInteger(WORKER_STATE_INIT); // 0 - init, 1 - started, 2 - shut down
private final long tickDuration;
private final HashedWheelBucket[] wheel;
@@ -207,7 +204,7 @@ public class HashedWheelTimer extends AbstractWheelTimer {
public void start() {
switch (workerState.get()) {
case WORKER_STATE_INIT:
if (workerState.compareAndSet(WORKER_STATE_INIT, WORKER_STATE_STARTED)) {
if (changeState(WORKER_STATE_INIT, WORKER_STATE_STARTED)) {
workerThread.start();
}
break;
@@ -238,7 +235,7 @@ public class HashedWheelTimer extends AbstractWheelTimer {
TimerTask.class.getSimpleName());
}
if (!workerState.compareAndSet(WORKER_STATE_STARTED, WORKER_STATE_SHUTDOWN)) {
if (!changeState(WORKER_STATE_STARTED, WORKER_STATE_SHUTDOWN)) {
// state is init or shutdown .
return Collections.emptySet();
}
@@ -315,7 +312,6 @@ public class HashedWheelTimer extends AbstractWheelTimer {
startTimeInitialized.countDown();
do {
//TODO 时间轮这里一直执行,结束不了任务
final long deadline = waitForNextTick();
if (deadline > 0) {
int idx = (int) (tick & mask);