Last active
September 26, 2023 10:20
-
-
Save julianjupiter/1e00edf3246981eb4dffa8c01a63543e to your computer and use it in GitHub Desktop.
Task scheduler in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.julianjupiter.scheduler; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.ScheduledFuture; | |
import java.util.concurrent.TimeUnit; | |
public class Scheduler { | |
private static final int DEFAULT_CORE_POOL_SIZE = 1; | |
private final ScheduledExecutorService scheduler; | |
private Runnable command; | |
private int initialDelay; | |
private long period; | |
private TimeUnit unit; | |
private boolean cancellable = false; | |
private int cancelDelay; | |
private TimeUnit cancelDelayUnit; | |
private Scheduler(int corePoolSize) { | |
this.scheduler = Executors.newScheduledThreadPool(corePoolSize); | |
} | |
public static Scheduler create() { | |
return new Scheduler(DEFAULT_CORE_POOL_SIZE); | |
} | |
public static Scheduler create(int corePoolSize) { | |
if (corePoolSize < DEFAULT_CORE_POOL_SIZE) { | |
return new Scheduler(DEFAULT_CORE_POOL_SIZE); | |
} | |
return new Scheduler(corePoolSize); | |
} | |
public Scheduler command(Runnable command) { | |
this.command = command; | |
return this; | |
} | |
public Scheduler initialDelay(int initialDelay) { | |
this.initialDelay = initialDelay; | |
return this; | |
} | |
public Scheduler period(long period) { | |
this.period = period; | |
return this; | |
} | |
public Scheduler unit(TimeUnit unit) { | |
this.unit = unit; | |
return this; | |
} | |
public Scheduler cancellable(int delay, TimeUnit unit) { | |
this.cancellable = true; | |
this.cancelDelay = delay; | |
this.cancelDelayUnit = unit; | |
return this; | |
} | |
public void run() { | |
final ScheduledFuture<?> beeperHandle = this.scheduler.scheduleAtFixedRate(this.command, this.initialDelay, this.period, this.unit); | |
if (cancellable) { | |
scheduler.schedule(() -> beeperHandle.cancel(true), this.cancelDelay, this.cancelDelayUnit); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.julianjupiter.scheduler.Scheduler; | |
import java.time.OffsetDateTime; | |
import java.util.UUID; | |
import java.util.concurrent.TimeUnit; | |
/* | |
* After 10 seconds, execute task every 5 seconds | |
*/ | |
public class SchedulerApp1 { | |
public static void main(String[] args) { | |
Scheduler.create() | |
.command(() -> System.out.println(UUID.randomUUID() + "|" + OffsetDateTime.now())) | |
.initialDelay(10) | |
.period(5) | |
.unit(TimeUnit.SECONDS) | |
.run(); | |
} | |
} | |
// Output | |
// $ java SchedulerApp1 | |
// 316aa114-0171-4419-8f82-2e8d6416a9d7|2023-09-26T18:05:59.638527300+08:00 | |
// b4960206-6db9-4d2e-b373-7d7f5170c91c|2023-09-26T18:06:03.789365700+08:00 | |
// 70ed583e-a918-4aa4-82f7-4068eb576945|2023-09-26T18:06:08.789587400+08:00 | |
// 92605d72-019a-44db-822b-70c565dd6d7e|2023-09-26T18:06:13.791974900+08:00 | |
// 68dca96c-522a-4eba-800d-4b47be54a44c|2023-09-26T18:06:18.801242+08:00 | |
// eb2c33fd-57ff-48e1-b83b-f5df4b43fd30|2023-09-26T18:06:23.786333900+08:00 | |
// 398e6722-5255-4184-bbb1-acd1b29ab895|2023-09-26T18:06:28.796843100+08:00 | |
// 36c8d420-9632-4895-b853-1f1dde136001|2023-09-26T18:06:33.796179500+08:00 | |
// 10405bf6-9ee3-4f3d-99ed-a21a2024ebd6|2023-09-26T18:06:38.789409+08:00 | |
// More... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.julianjupiter.scheduler.Scheduler; | |
import java.time.OffsetDateTime; | |
import java.util.UUID; | |
import java.util.concurrent.TimeUnit; | |
/* | |
* After 10 seconds, execute task every 5 seconds. | |
* Stop execution of tasks after 28 seconds, hence, only 4 tasks executed. | |
*/ | |
public class SchedulerApp2 { | |
public static void main(String[] args) { | |
Scheduler.create() | |
.command(() -> System.out.println(UUID.randomUUID() + "|" + OffsetDateTime.now())) | |
.initialDelay(10) | |
.period(5) | |
.unit(TimeUnit.SECONDS) | |
.cancellable(28, TimeUnit.SECONDS) | |
.run(); | |
} | |
} | |
// Output | |
// $ java SchedulerApp2 | |
// 67358954-7b98-404f-b8b7-c1a30f4c1e20|2023-09-26T18:07:24.465679100+08:00 | |
// 4f8e4ac9-3111-4c8f-ba32-89750aff6e58|2023-09-26T18:07:28.720129300+08:00 | |
// 315a84f3-084d-40eb-90f0-48409eb6548c|2023-09-26T18:07:33.719566300+08:00 | |
// 700fbe1c-c431-43ce-bcdb-320e3ffbb0bc|2023-09-26T18:07:38.716771900+08:00 | |
// 5440eb22-dd8f-4ca8-93fe-0d6ffddc87e4|2023-09-26T18:07:43.709422400+08:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment