Java's ScheduledExecutorService example

Example of Java's ScheduledExecutorService to run a block of code once (with a delay) or repeatedly at fixed rate or fixed delay.

ScheduledExecutorService can be a single threaded executor (used to execute in a coordinated way) or can be multi threaded executor (used to execute in parallel)

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ExampleScheduledExecutor implements AutoCloseable {

private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
// use newScheduledThreadPool() for running with multiple threads in parallel
// private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);

public ExampleScheduledExecutor(long initialDelay, long fixedDelay) {
executorService.schedule(() -> executeOnlyOnce(), initialDelay, TimeUnit.SECONDS);
executorService.scheduleAtFixedRate(() -> executeAtFixedRate(), initialDelay, fixedDelay, TimeUnit.SECONDS);
executorService.scheduleWithFixedDelay(() -> executeAtFixedDelay(), initialDelay, fixedDelay, TimeUnit.SECONDS);
}

private void executeOnlyOnce() {
System.out.println("Executed only once after delay");
}

private void executeAtFixedRate() {
System.out.println("Executed at fixed rate");
}

private void executeAtFixedDelay() {
System.out.println("Executed at fixed delay");
}

@Override
public void close() throws Exception {
System.out.println("Shutting down executor service to free up resources");
executorService.shutdown();
System.out.println("Shut down of executor service completed");
}
}

If used in projects with Spring Framework,
then DisposableBean interface can be used instead of AutoCloseable

import org.springframework.beans.factory.DisposableBean;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ExampleScheduledExecutorSpring implements DisposableBean {

private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
// use newScheduledThreadPool() for running with multiple threads in parallel
// private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);

public ExampleScheduledExecutorSpring(long initialDelay, long fixedDelay) {
executorService.schedule(() -> executeOnlyOnce(), initialDelay, TimeUnit.SECONDS);
executorService.scheduleAtFixedRate(() -> executeAtFixedRate(), initialDelay, fixedDelay, TimeUnit.SECONDS);
executorService.scheduleWithFixedDelay(() -> executeAtFixedDelay(), initialDelay, fixedDelay, TimeUnit.SECONDS);
}

private void executeOnlyOnce() {
System.out.println("Executed only once after delay");
}

private void executeAtFixedRate() {
System.out.println("Executed at fixed rate");
}

private void executeAtFixedDelay() {
System.out.println("Executed at fixed delay");
}

@Override
public void destroy() throws Exception {
System.out.println("Shutting down executor service to free up resources");
executorService.shutdown();
System.out.println("Shut down of executor service completed");
}
}

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux