mirror of
https://gitee.com/52itstyle/spring-boot-seckill.git
synced 2025-12-30 10:22:26 +00:00
:sparkles:基于 netty 的时间轮算法 HashedWheelTimer 实现的延迟任务
This commit is contained in:
@@ -4,6 +4,8 @@ import io.netty.util.HashedWheelTimer;
|
||||
import io.netty.util.Timer;
|
||||
import io.netty.util.TimerTask;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -13,6 +15,8 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class RedPacketHashedWheelTimer {
|
||||
|
||||
private static final DateTimeFormatter F = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ThreadFactory factory = r -> {
|
||||
Thread thread = new Thread(r);
|
||||
@@ -24,11 +28,12 @@ public class RedPacketHashedWheelTimer {
|
||||
* @param tickDuration - 每tick一次的时间间隔
|
||||
* @param unit - tickDuration 的时间单位
|
||||
* @param ticksPerWheel - 时间轮中的槽数
|
||||
* @param leakDetection
|
||||
* @param leakDetection - 检查内存溢出
|
||||
*/
|
||||
Timer timer = new HashedWheelTimer(factory, 1,
|
||||
TimeUnit.SECONDS, 100,true);
|
||||
for(int i=0;i<100;i++){
|
||||
System.out.println(String.format("开始任务时间:%s",LocalDateTime.now().format(F)));
|
||||
for(int i=1;i<10;i++){
|
||||
TimerTask timerTask = new RedPacketTimerTask(i);
|
||||
timer.newTimeout(timerTask, i, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 红包过期队列信息
|
||||
*/
|
||||
public class RedPacketTimerTask implements TimerTask {
|
||||
|
||||
private static final DateTimeFormatter F = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
Reference in New Issue
Block a user