Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Heartbeat Timing #16

Open
Trip-kun opened this issue Nov 3, 2023 · 0 comments
Open

Heartbeat Timing #16

Trip-kun opened this issue Nov 3, 2023 · 0 comments
Assignees
Labels
help wanted Extra attention is needed Improvement A feature works, but it could work better

Comments

@Trip-kun
Copy link

Trip-kun commented Nov 3, 2023

Improvements can be made to Heartbeat as sleep(300000) does not work great as the time spent processing one iteration delays the next.
From Coderabbitai:

The thread is put to sleep for a fixed interval. If the processing of reminders takes a significant amount of time, the actual interval between heartbeats could be much longer than intended. Consider using a ScheduledExecutorService to schedule the heartbeat at fixed intervals.

Here's a brief overview of how you might use a ScheduledExecutorService:

First, you'll need to import it: import java.util.concurrent.ScheduledExecutorService;
Then, you can create a new ScheduledExecutorService with a fixed thread pool size. For example, if you want a single thread: ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
You can then schedule your task to run at fixed intervals. For example, if you want your task to run every 5 minutes, regardless of how long the task takes to execute: executorService.scheduleAtFixedRate(this::run, 0, 5, TimeUnit.MINUTES);
Remember to properly shut down your executor service when you're done with it to free up resources: executorService.shutdown();

This way, the run() method will be called every 5 minutes, regardless of how long the previous execution took. This should help keep your heartbeat more consistent. Let me know if you have any questions!

@Trip-kun Trip-kun added help wanted Extra attention is needed Improvement A feature works, but it could work better labels Nov 3, 2023
@Trip-kun Trip-kun self-assigned this Nov 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed Improvement A feature works, but it could work better
Projects
None yet
Development

No branches or pull requests

1 participant