作者 crossoverJie

:bug: fix add repeat delay job exception

... ... @@ -45,6 +45,13 @@ public final class RingBufferWheel {
*/
private volatile boolean stop = false;
private volatile boolean start = false ;
/**
* total tick times
*/
private AtomicInteger tick = new AtomicInteger() ;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
... ... @@ -112,10 +119,15 @@ public final class RingBufferWheel {
* Start background thread to consumer wheel timer, it will always run until you call method {@link #stop}
*/
public void start() {
logger.info("delay task is starting");
Thread job = new Thread(new TriggerJob());
job.setName("consumer RingBuffer thread");
job.start();
if (!start){
logger.info("delay task is starting");
Thread job = new Thread(new TriggerJob());
job.setName("consumer RingBuffer thread");
job.start();
start = true ;
}
}
/**
... ... @@ -211,6 +223,7 @@ public final class RingBufferWheel {
private int mod(int target, int mod) {
// equals target % mod
target = target + tick.get() ;
return target & (mod - 1);
}
... ... @@ -267,6 +280,8 @@ public final class RingBufferWheel {
index = 0;
}
//Total tick number of records
tick.incrementAndGet();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
... ...
... ... @@ -12,7 +12,7 @@ public class RingBufferWheelTest {
private static Logger logger = LoggerFactory.getLogger(RingBufferWheelTest.class) ;
public static void main(String[] args) throws InterruptedException {
test5();
test6();
return;
}
... ... @@ -124,6 +124,31 @@ public class RingBufferWheelTest {
}
private static void test6() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(2) ;
RingBufferWheel wheel = new RingBufferWheel(executorService,512) ;
for (int i = 0; i < 10; i++) {
RingBufferWheel.Task task = new Job(i) ;
task.setKey(i);
wheel.addTask(task);
}
wheel.start();
TimeUnit.SECONDS.sleep(10);
RingBufferWheel.Task task = new Job(15) ;
task.setKey(15);
wheel.addTask(task);
wheel.start();
logger.info("task size={}",wheel.taskSize());
wheel.stop(false);
}
private static class Job extends RingBufferWheel.Task{
... ...