作者 crossoverJie

:white_check_mark: Adding tests.

... ... @@ -41,10 +41,15 @@ public final class RingBufferWheel {
private AtomicInteger taskSize = new AtomicInteger();
/***
* task running sign
* task stop sign
*/
private volatile boolean stop = false;
/**
* task start sign
*/
private volatile boolean start = false ;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
... ... @@ -98,6 +103,7 @@ public final class RingBufferWheel {
taskSize.incrementAndGet();
start();
}
/**
... ... @@ -112,10 +118,13 @@ public final class RingBufferWheel {
* Start background thread to consumer wheel timer, it will 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 ;
}
}
/**
... ...
package com.crossoverjie.cim.common.data.construct;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -13,7 +12,7 @@ public class RingBufferWheelTest {
private static Logger logger = LoggerFactory.getLogger(RingBufferWheelTest.class) ;
public static void main(String[] args) throws InterruptedException {
test5();
test1();
return;
}
... ... @@ -30,8 +29,6 @@ public class RingBufferWheelTest {
task.setKey(74);
wheel.addTask(task) ;
wheel.start();
while (true){
logger.info("task size={}" , wheel.taskSize());
TimeUnit.SECONDS.sleep(1);
... ... @@ -117,8 +114,6 @@ public class RingBufferWheelTest {
wheel.addTask(task);
}
wheel.start();
logger.info("task size={}",wheel.taskSize());
wheel.stop(false);
... ... @@ -134,6 +129,32 @@ public class RingBufferWheelTest {
logger.info("================");
}
}
private static void cuncrrentTest6() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10) ;
RingBufferWheel wheel = new RingBufferWheel(executorService) ;
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{
... ...