Skip to content

Commit a19abe9

Browse files
author
Nothere998
authored
Scheduler pattern
The Scheduler design pattern provides a clean and structured solution to manage and execute tasks automatically. It involves: Scheduler: A component responsible for managing the execution of tasks, ensuring they run at the correct time or under the right conditions. Task: A unit of work that encapsulates the logic to be executed. Trigger/Condition: Defines when and how often a task should execute, whether it's based on time intervals (e.g., every minute) or events Execution Context: Ensures that tasks are executed in the right environment, managing resources, states, and concurrency. By using this pattern, tasks are managed centrally, execution timing is automated, and the system can handle various triggers and execution conditions efficiently, allowing developers to focus on defining what needs to be done instead of when and how to execute it.
1 parent 5171a96 commit a19abe9

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Scheduler pattern

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
interface Job {
4+
void execute();
5+
}
6+
interface Trigger {
7+
boolean shouldRun();
8+
}
9+
class Scheduler {
10+
private List<Job> jobs;
11+
private List<Trigger> triggers;
12+
public Scheduler() {
13+
this.jobs = new ArrayList<>();
14+
this.triggers = new ArrayList<>();
15+
}
16+
public void addJob(Job job, Trigger trigger) {
17+
jobs.add(job);
18+
triggers.add(trigger);
19+
}
20+
public void start() {
21+
while (true) {
22+
for (int i = 0; i < jobs.size(); i++) {
23+
Job job = jobs.get(i);
24+
Trigger trigger = triggers.get(i);
25+
26+
if (trigger.shouldRun()) {
27+
job.execute();
28+
}
29+
}
30+
try {
31+
Thread.sleep(1000); // Delay for 1 second
32+
} catch (InterruptedException e) {
33+
Thread.currentThread().interrupt();
34+
}
35+
}
36+
}
37+
}
38+
class ExampleJob implements Job {
39+
@Override
40+
public void execute() {
41+
System.out.println("Executing ExampleJob at " + System.currentTimeMillis());
42+
}
43+
}
44+
class TimeBasedTrigger implements Trigger {
45+
private long lastExecutionTime;
46+
private long interval;
47+
public TimeBasedTrigger(long interval) {
48+
this.interval = interval;
49+
this.lastExecutionTime = System.currentTimeMillis();
50+
}
51+
@Override
52+
public boolean shouldRun() {
53+
long currentTime = System.currentTimeMillis();
54+
if (currentTime - lastExecutionTime >= interval) {
55+
lastExecutionTime = currentTime;
56+
return true;
57+
}
58+
return false;
59+
}
60+
}
61+
public class JobSDesign {
62+
public static void main(String[] args) {
63+
Scheduler scheduler = new Scheduler();
64+
Job exampleJob = new ExampleJob();
65+
Trigger timeBasedTrigger = new TimeBasedTrigger(5000); // Run every 5 seconds
66+
scheduler.addJob(exampleJob, timeBasedTrigger);
67+
scheduler.start();
68+
}
69+
}

0 commit comments

Comments
 (0)