forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancedTaskScheduler.java
More file actions
493 lines (428 loc) · 18 KB
/
AdvancedTaskScheduler.java
File metadata and controls
493 lines (428 loc) · 18 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* Advanced Task Scheduler Implementation
*
* This program demonstrates advanced concepts including:
* - Thread Pool Management with Custom ThreadPoolExecutor
* - Priority-based Task Scheduling using PriorityBlockingQueue
* - Task Dependencies and Execution Chains
* - Monitoring and Statistics Collection
* - Graceful Shutdown with Resource Management
* - Custom Task Types with different behaviors
* - Thread-safe operations and concurrent programming
*
* Features:
* 1. Priority-based task execution
* 2. Scheduled tasks with delays and intervals
* 3. Task dependency management
* 4. Real-time performance monitoring
* 5. Dynamic thread pool scaling
* 6. Task retry mechanism with exponential backoff
*
* @author Contributing to Hacktoberfest 2025
* @version 2.0
*/
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* Represents a task with priority, dependencies, and execution context
*/
class AdvancedTask implements Comparable<AdvancedTask> {
private static final AtomicLong taskIdGenerator = new AtomicLong(0);
private final long taskId;
private final String name;
private final Runnable task;
private final int priority; // Higher number = higher priority
private final List<Long> dependencies;
private final long scheduledTime;
private final int maxRetries;
private final long retryDelay;
private volatile TaskStatus status;
private volatile int attemptCount;
private volatile String errorMessage;
private volatile long executionTime;
public enum TaskStatus {
PENDING, RUNNING, COMPLETED, FAILED, CANCELLED, WAITING_FOR_DEPENDENCIES
}
public AdvancedTask(String name, Runnable task, int priority) {
this(name, task, priority, new ArrayList<>(), 0, 3, 1000);
}
public AdvancedTask(String name, Runnable task, int priority, List<Long> dependencies,
long delayMs, int maxRetries, long retryDelay) {
this.taskId = taskIdGenerator.incrementAndGet();
this.name = name;
this.task = task;
this.priority = priority;
this.dependencies = new ArrayList<>(dependencies);
this.scheduledTime = System.currentTimeMillis() + delayMs;
this.maxRetries = maxRetries;
this.retryDelay = retryDelay;
this.status = TaskStatus.PENDING;
this.attemptCount = 0;
}
@Override
public int compareTo(AdvancedTask other) {
// First compare by scheduled time (earlier tasks first)
int timeComparison = Long.compare(this.scheduledTime, other.scheduledTime);
if (timeComparison != 0) return timeComparison;
// Then by priority (higher priority first)
int priorityComparison = Integer.compare(other.priority, this.priority);
if (priorityComparison != 0) return priorityComparison;
// Finally by task ID (FIFO for same priority and time)
return Long.compare(this.taskId, other.taskId);
}
// Getters and setters
public long getTaskId() { return taskId; }
public String getName() { return name; }
public Runnable getTask() { return task; }
public int getPriority() { return priority; }
public List<Long> getDependencies() { return new ArrayList<>(dependencies); }
public long getScheduledTime() { return scheduledTime; }
public int getMaxRetries() { return maxRetries; }
public long getRetryDelay() { return retryDelay; }
public TaskStatus getStatus() { return status; }
public void setStatus(TaskStatus status) { this.status = status; }
public int getAttemptCount() { return attemptCount; }
public void incrementAttemptCount() { this.attemptCount++; }
public String getErrorMessage() { return errorMessage; }
public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
public long getExecutionTime() { return executionTime; }
public void setExecutionTime(long executionTime) { this.executionTime = executionTime; }
}
/**
* Thread Pool Statistics for monitoring
*/
class ThreadPoolStats {
private final AtomicLong tasksExecuted = new AtomicLong(0);
private final AtomicLong tasksCompleted = new AtomicLong(0);
private final AtomicLong tasksFailed = new AtomicLong(0);
private final AtomicLong totalExecutionTime = new AtomicLong(0);
private final Map<String, AtomicLong> taskTypeCount = new ConcurrentHashMap<>();
public void recordTaskExecution(String taskName, long executionTime, boolean success) {
tasksExecuted.incrementAndGet();
if (success) {
tasksCompleted.incrementAndGet();
} else {
tasksFailed.incrementAndGet();
}
totalExecutionTime.addAndGet(executionTime);
taskTypeCount.computeIfAbsent(taskName, k -> new AtomicLong(0)).incrementAndGet();
}
public void printStats() {
System.out.println("\n=== Thread Pool Statistics ===");
System.out.println("Tasks Executed: " + tasksExecuted.get());
System.out.println("Tasks Completed: " + tasksCompleted.get());
System.out.println("Tasks Failed: " + tasksFailed.get());
System.out.println("Average Execution Time: " +
(tasksExecuted.get() > 0 ? totalExecutionTime.get() / tasksExecuted.get() : 0) + "ms");
System.out.println("Success Rate: " +
(tasksExecuted.get() > 0 ? (tasksCompleted.get() * 100.0 / tasksExecuted.get()) : 0) + "%");
System.out.println("Task Type Distribution:");
taskTypeCount.forEach((type, count) ->
System.out.println(" " + type + ": " + count.get()));
}
}
/**
* Main Task Scheduler Implementation
*/
public class AdvancedTaskScheduler {
private final ThreadPoolExecutor executor;
private final PriorityBlockingQueue<AdvancedTask> taskQueue;
private final ScheduledExecutorService schedulerService;
private final Map<Long, AdvancedTask> allTasks;
private final Set<Long> completedTasks;
private final ThreadPoolStats stats;
private volatile boolean shutdown = false;
public AdvancedTaskScheduler(int corePoolSize, int maximumPoolSize) {
this.taskQueue = new PriorityBlockingQueue<>();
this.executor = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
new CustomThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy()
);
this.schedulerService = Executors.newScheduledThreadPool(2);
this.allTasks = new ConcurrentHashMap<>();
this.completedTasks = ConcurrentHashMap.newKeySet();
this.stats = new ThreadPoolStats();
// Start the main scheduling loop
schedulerService.scheduleAtFixedRate(this::processTaskQueue, 0, 100, TimeUnit.MILLISECONDS);
// Start statistics reporting
schedulerService.scheduleAtFixedRate(stats::printStats, 5, 5, TimeUnit.SECONDS);
}
/**
* Custom ThreadFactory for better thread naming and monitoring
*/
private static class CustomThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
CustomThreadFactory() {
namePrefix = "AdvancedScheduler-" + poolNumber.getAndIncrement() + "-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, namePrefix + threadNumber.getAndIncrement());
if (t.isDaemon()) t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
/**
* Submit a task for execution
*/
public long submitTask(AdvancedTask task) {
if (shutdown) {
throw new IllegalStateException("Scheduler is shutdown");
}
allTasks.put(task.getTaskId(), task);
taskQueue.offer(task);
System.out.printf("[%s] Task '%s' (ID: %d, Priority: %d) submitted%n",
getCurrentTime(), task.getName(), task.getTaskId(), task.getPriority());
return task.getTaskId();
}
/**
* Process the task queue and execute ready tasks
*/
private void processTaskQueue() {
if (shutdown) return;
List<AdvancedTask> readyTasks = new ArrayList<>();
AdvancedTask task;
// Collect all ready tasks
while ((task = taskQueue.poll()) != null) {
if (isTaskReady(task)) {
readyTasks.add(task);
} else {
// Put back in queue if not ready
taskQueue.offer(task);
break; // Queue is ordered, so if this isn't ready, neither are the rest
}
}
// Execute ready tasks
for (AdvancedTask readyTask : readyTasks) {
executeTask(readyTask);
}
}
/**
* Check if a task is ready for execution
*/
private boolean isTaskReady(AdvancedTask task) {
// Check if scheduled time has arrived
if (System.currentTimeMillis() < task.getScheduledTime()) {
return false;
}
// Check if dependencies are satisfied
for (Long depId : task.getDependencies()) {
if (!completedTasks.contains(depId)) {
task.setStatus(AdvancedTask.TaskStatus.WAITING_FOR_DEPENDENCIES);
return false;
}
}
return true;
}
/**
* Execute a task with error handling and retry logic
*/
private void executeTask(AdvancedTask task) {
task.setStatus(AdvancedTask.TaskStatus.RUNNING);
task.incrementAttemptCount();
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
long startTime = System.currentTimeMillis();
try {
System.out.printf("[%s] Executing task '%s' (Attempt %d/%d)%n",
getCurrentTime(), task.getName(), task.getAttemptCount(), task.getMaxRetries());
task.getTask().run();
long executionTime = System.currentTimeMillis() - startTime;
task.setExecutionTime(executionTime);
task.setStatus(AdvancedTask.TaskStatus.COMPLETED);
completedTasks.add(task.getTaskId());
stats.recordTaskExecution(task.getName(), executionTime, true);
System.out.printf("[%s] Task '%s' completed in %dms%n",
getCurrentTime(), task.getName(), executionTime);
} catch (Exception e) {
long executionTime = System.currentTimeMillis() - startTime;
task.setExecutionTime(executionTime);
task.setErrorMessage(e.getMessage());
System.err.printf("[%s] Task '%s' failed: %s%n",
getCurrentTime(), task.getName(), e.getMessage());
// Retry logic with exponential backoff
if (task.getAttemptCount() < task.getMaxRetries()) {
long delay = task.getRetryDelay() * (1L << (task.getAttemptCount() - 1)); // Exponential backoff
System.out.printf("[%s] Retrying task '%s' in %dms%n",
getCurrentTime(), task.getName(), delay);
schedulerService.schedule(() -> {
task.setStatus(AdvancedTask.TaskStatus.PENDING);
taskQueue.offer(task);
}, delay, TimeUnit.MILLISECONDS);
} else {
task.setStatus(AdvancedTask.TaskStatus.FAILED);
stats.recordTaskExecution(task.getName(), executionTime, false);
System.err.printf("[%s] Task '%s' failed permanently after %d attempts%n",
getCurrentTime(), task.getName(), task.getAttemptCount());
}
}
}, executor);
// Handle uncaught exceptions
future.exceptionally(throwable -> {
System.err.printf("[%s] Uncaught exception in task '%s': %s%n",
getCurrentTime(), task.getName(), throwable.getMessage());
task.setStatus(AdvancedTask.TaskStatus.FAILED);
return null;
});
}
/**
* Get current formatted time
*/
private String getCurrentTime() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"));
}
/**
* Get task status by ID
*/
public AdvancedTask.TaskStatus getTaskStatus(long taskId) {
AdvancedTask task = allTasks.get(taskId);
return task != null ? task.getStatus() : null;
}
/**
* Get detailed task information
*/
public AdvancedTask getTaskInfo(long taskId) {
return allTasks.get(taskId);
}
/**
* Graceful shutdown
*/
public void shutdown() {
shutdown = true;
System.out.println("\n[" + getCurrentTime() + "] Initiating graceful shutdown...");
schedulerService.shutdown();
executor.shutdown();
try {
if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
if (!schedulerService.awaitTermination(10, TimeUnit.SECONDS)) {
schedulerService.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
schedulerService.shutdownNow();
Thread.currentThread().interrupt();
}
stats.printStats();
System.out.println("[" + getCurrentTime() + "] Shutdown completed");
}
/**
* Main method demonstrating the Advanced Task Scheduler
*/
public static void main(String[] args) throws InterruptedException {
System.out.println("=== Advanced Task Scheduler Demo ===\n");
AdvancedTaskScheduler scheduler = new AdvancedTaskScheduler(4, 8);
// Demo tasks with different priorities and characteristics
// High priority task
long task1 = scheduler.submitTask(new AdvancedTask(
"DataProcessing",
() -> {
try {
Thread.sleep(2000); // Simulate work
System.out.println(" -> Processing large dataset...");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
},
10
));
// Medium priority task that depends on task1
long task2 = scheduler.submitTask(new AdvancedTask(
"ReportGeneration",
() -> {
try {
Thread.sleep(1500);
System.out.println(" -> Generating comprehensive report...");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
},
5,
Arrays.asList(task1), // Depends on task1
0,
2,
500
));
// Low priority scheduled task
scheduler.submitTask(new AdvancedTask(
"SystemCleanup",
() -> {
System.out.println(" -> Performing system cleanup...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
},
1,
new ArrayList<>(),
3000, // 3 second delay
3,
1000
));
// Task that will fail and retry
scheduler.submitTask(new AdvancedTask(
"NetworkOperation",
() -> {
System.out.println(" -> Attempting network connection...");
if (Math.random() < 0.7) { // 70% chance to fail
throw new RuntimeException("Network timeout");
}
System.out.println(" -> Network operation successful!");
},
7,
new ArrayList<>(),
1000,
4,
2000
));
// CPU intensive task
scheduler.submitTask(new AdvancedTask(
"CpuIntensiveTask",
() -> {
System.out.println(" -> Starting CPU intensive calculation...");
long start = System.currentTimeMillis();
// Simulate CPU work
long result = 0;
for (int i = 0; i < 10000000; i++) {
result += Math.sqrt(i);
}
long duration = System.currentTimeMillis() - start;
System.out.printf(" -> CPU task completed in %dms (result: %.2f)%n", duration, result);
},
3
));
// Multiple quick tasks
for (int i = 1; i <= 5; i++) {
final int taskNum = i;
scheduler.submitTask(new AdvancedTask(
"QuickTask" + taskNum,
() -> {
System.out.printf(" -> Quick task %d executed%n", taskNum);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
},
2
));
}
System.out.println("\nAll tasks submitted. Processing...\n");
// Let the scheduler run for a while
Thread.sleep(15000);
// Graceful shutdown
scheduler.shutdown();
}
}