Search code examples
javamultithreadingquasar

Scheduling tasks in Quasar, without spawning a new thread


I'd like to spawn many Actors which execute a private method at a specific time interval. This task will need to run at the time interval indefinitely until the Actor is terminated.

All of the solutions I've seen in Java involve creating a new thread, for example Spring's TaskExecutor or using ScheduledExecutorService. Because an Actor already has its own Fiber, I see no reason to spawn a thread unless my task is very heavy.

In Elixir, this was very simple to achieve using Process.send_after().

Is there a similar way to do this in Java using Quasar?


Solution

  • Fiber has method sleep(long millis), so you can easily create a fiber which executes periodically with:

    while (!end) {
        doTask();
        sleep(period);
    }