Skip to content

Commit

Permalink
refactor(kit): TaskKit supports setting Timer
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Jan 5, 2025
1 parent 0d3b3f7 commit 831fb21
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import com.iohao.game.common.kit.collect.SetMultiMap;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;
import lombok.Getter;
import lombok.experimental.UtilityClass;

import java.util.Objects;
import java.util.Set;
import java.util.concurrent.*;
import java.util.function.Supplier;
Expand Down Expand Up @@ -142,7 +144,7 @@
@UtilityClass
public class TaskKit {
/** 执行一些没有 io 操作的逻辑 */
private final HashedWheelTimer wheelTimer = new HashedWheelTimer();
private Timer wheelTimer = new HashedWheelTimer();
/** 内置的 cacheExecutor 执行器 */
@Getter
final ExecutorService cacheExecutor = ExecutorKit.newFixedThreadPool(RuntimeKit.availableProcessors2n, "ioGameThread-");
Expand All @@ -154,6 +156,24 @@ public class TaskKit {
record TickTimeUnit(long tick, TimeUnit timeUnit) {
}

/**
* set HashedWheelTimer
* <pre>{@code
* TaskKit.setTimer(new HashedWheelTimer(17, TimeUnit.MILLISECONDS));
* }
* </pre>
*
* @param timer Timer
* @since 21.23
*/
public void setTimer(Timer timer) {
Objects.requireNonNull(timer);

var oldTimer = wheelTimer;
wheelTimer = timer;
oldTimer.stop();
}

/**
* 使用其他线程执行任务
*
Expand Down

0 comments on commit 831fb21

Please sign in to comment.