-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
90 lines (70 loc) · 4.11 KB
/
Main.java
File metadata and controls
90 lines (70 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.taskscheduler;
import java.util.concurrent.TimeUnit;
/**
* Demonstrates the custom ThreadPool and ScheduledTask system.
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println("╔══════════════════════════════════════╗");
System.out.println("║ Custom Thread Pool Task Scheduler ║");
System.out.println("╚══════════════════════════════════════╝\n");
// ── Demo 1: Basic task submission ─────────────────────────────
System.out.println("--- Demo 1: Basic Task Submission ---");
ThreadPool pool = new ThreadPool(3, 50);
for (int i = 1; i <= 6; i++) {
final int taskNum = i;
pool.submit("BasicTask-" + i, () -> {
System.out.printf(" [Task %d] Running on %s%n", taskNum, Thread.currentThread().getName());
sleep(200);
});
}
Thread.sleep(2000);
// ── Demo 2: Priority tasks ─────────────────────────────────────
System.out.println("\n--- Demo 2: Priority Task Ordering ---");
ThreadPool priorityPool = new ThreadPool(1, 20); // 1 worker so ordering is visible
// Submit low priority first, high priority last — high should run first
priorityPool.submit("Low-Priority-Task", () -> sleep(100), Task.Priority.LOW);
priorityPool.submit("Low-Priority-Task-2", () -> sleep(100), Task.Priority.LOW);
priorityPool.submit("Medium-Priority-Task", () -> sleep(100), Task.Priority.MEDIUM);
priorityPool.submit("HIGH-PRIORITY-TASK", () -> sleep(100), Task.Priority.HIGH);
priorityPool.submit("HIGH-PRIORITY-TASK-2", () -> sleep(100), Task.Priority.HIGH);
Thread.sleep(1500);
priorityPool.shutdown();
// ── Demo 3: Scheduled tasks ────────────────────────────────────
System.out.println("\n--- Demo 3: Scheduled Tasks ---");
ThreadPool scheduledPool = new ThreadPool(2);
ScheduledTask scheduler = new ScheduledTask(scheduledPool);
scheduler.scheduleOnce("Delayed-Report", () ->
System.out.println(" [Delayed-Report] Running 1 second after scheduling!"),
1, TimeUnit.SECONDS
);
scheduler.scheduleRepeating("Heartbeat", () ->
System.out.println(" [Heartbeat] Ping! " + System.currentTimeMillis()),
0, 500, TimeUnit.MILLISECONDS
);
Thread.sleep(2500);
scheduler.shutdown();
scheduledPool.shutdown();
// ── Demo 4: Stress test ────────────────────────────────────────
System.out.println("\n--- Demo 4: Stress Test (50 tasks, 4 workers) ---");
ThreadPool stressPool = new ThreadPool(4, 100);
long startTime = System.currentTimeMillis();
for (int i = 1; i <= 50; i++) {
final int num = i;
Task.Priority priority = num % 3 == 0 ? Task.Priority.HIGH
: num % 2 == 0 ? Task.Priority.MEDIUM
: Task.Priority.LOW;
stressPool.submit("StressTask-" + num, () -> sleep(50), priority);
}
stressPool.shutdown();
long elapsed = System.currentTimeMillis() - startTime;
System.out.printf("Stress test completed in %dms%n", elapsed);
// ── Final pool shutdown ────────────────────────────────────────
pool.shutdown();
System.out.println("\n✅ All demos complete!");
}
private static void sleep(long ms) {
try { Thread.sleep(ms); }
catch (InterruptedException e) { Thread.currentThread().interrupt(); }
}
}