Add scheduled task lifecycle
This commit is contained in:
parent
279fad3431
commit
2e2ec6ff68
2
pom.xml
2
pom.xml
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
<artifactId>common-utils</artifactId>
|
<artifactId>common-utils</artifactId>
|
||||||
<groupId>org.warp</groupId>
|
<groupId>org.warp</groupId>
|
||||||
<version>1.1.1</version>
|
<version>1.1.2</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
package org.warp.commonutils.concurrency.executor;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.locks.StampedLock;
|
||||||
|
import org.warp.commonutils.concurrency.atomicity.Atomic;
|
||||||
|
|
||||||
|
@Atomic
|
||||||
|
public class ScheduledTaskLifecycle {
|
||||||
|
|
||||||
|
private final StampedLock lock;
|
||||||
|
private final ConcurrentHashMap<ScheduledFuture<?>, Object> tasks = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public ScheduledTaskLifecycle() {
|
||||||
|
this.lock = new StampedLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerScheduledTask(ScheduledFuture<?> task) {
|
||||||
|
this.tasks.put(task, new Object());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startScheduledTask() {
|
||||||
|
this.lock.readLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void endScheduledTask() {
|
||||||
|
this.lock.tryUnlockRead();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancelAndWait() {
|
||||||
|
tasks.forEach((task, obj) -> {
|
||||||
|
task.cancel(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
lock.unlockWrite(lock.writeLock());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package org.warp.commonutils.concurrency.executor;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class TestScheduledTaskLifecycle {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testScheduledTaskLifecycle() throws InterruptedException {
|
||||||
|
var scheduler = Executors.newScheduledThreadPool(100);
|
||||||
|
|
||||||
|
var lifecycle = new ScheduledTaskLifecycle();
|
||||||
|
|
||||||
|
AtomicInteger runningTasks = new AtomicInteger();
|
||||||
|
|
||||||
|
for (int i = 0; i < 49; i++) {
|
||||||
|
lifecycle.registerScheduledTask(scheduler.scheduleAtFixedRate(() -> {
|
||||||
|
lifecycle.startScheduledTask();
|
||||||
|
runningTasks.incrementAndGet();
|
||||||
|
try {
|
||||||
|
Thread.sleep(33);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
runningTasks.decrementAndGet();
|
||||||
|
lifecycle.endScheduledTask();
|
||||||
|
}
|
||||||
|
}, 0, 1, TimeUnit.MICROSECONDS));
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.sleep(96);
|
||||||
|
|
||||||
|
lifecycle.cancelAndWait();
|
||||||
|
|
||||||
|
System.out.println("stopped");
|
||||||
|
|
||||||
|
Assertions.assertEquals(0, runningTasks.get());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user