Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Algorithm implementation of Ring queue efficiently triggering a large number of timeout tasks

Shulou Source: shulou.com Published: 2022-06-03 04:18:17 11月15日 Update

The timeout trigger algorithm based on ring queue only needs one timer to trigger batch timeout tasks with low CPU consumption and high efficiency. Introduction to the principle, the following is a simple implementation of this algorithm.

1,TaskHolder.java

Package com.zws.timer;/** @ author wensh.zhu * @ date 2018-04-22 * / public class TaskHolder {/ * * the number of laps the task needs to wait, that is, how many laps the task needs to go * * / private int cycles; private int delays; private Runnable task Public TaskHolder () {} public TaskHolder (int cycles, int delays, Runnable task) {this.cycles = cycles; this.delays = delays; this.task = task;} public boolean isTimeOut () {return cycles

Tags: Task timer algorithm time ring queue arrival unit principle efficiency state length station detection consumption Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Linux NVidia Xiaomi