-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.java
More file actions
115 lines (97 loc) · 3.27 KB
/
ThreadPool.java
File metadata and controls
115 lines (97 loc) · 3.27 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
package com.taskscheduler;
import java.util.ArrayList;
import java.util.List;
/**
* A custom thread pool implementation built from scratch.
*
* Features:
* - Configurable number of worker threads
* - Priority-based task queue
* - Graceful shutdown (drains queue before stopping)
* - Runtime statistics
*/
public class ThreadPool {
private final int poolSize;
private final TaskQueue taskQueue;
private final List<WorkerThread> workers;
private final ThreadPoolStats stats;
private volatile boolean isShutdown = false;
public ThreadPool(int poolSize, int queueCapacity) {
this.poolSize = poolSize;
this.taskQueue = new TaskQueue(queueCapacity);
this.workers = new ArrayList<>(poolSize);
this.stats = new ThreadPoolStats();
initWorkers();
}
public ThreadPool(int poolSize) {
this(poolSize, 100);
}
private void initWorkers() {
for (int i = 0; i < poolSize; i++) {
WorkerThread worker = new WorkerThread(taskQueue, stats);
workers.add(worker);
worker.start();
}
System.out.printf("[ThreadPool] Started with %d worker threads.%n", poolSize);
}
/**
* Submit a task to the pool.
*/
public boolean submit(Task task) {
if (isShutdown) {
System.err.println("[ThreadPool] Rejected — pool is shut down: " + task);
return false;
}
return taskQueue.submit(task);
}
/**
* Convenience method — submit by name and runnable with default priority.
*/
public boolean submit(String name, Runnable runnable) {
return submit(new Task(name, runnable));
}
/**
* Convenience method — submit with explicit priority.
*/
public boolean submit(String name, Runnable runnable, Task.Priority priority) {
return submit(new Task(name, runnable, priority));
}
/**
* Graceful shutdown — stops accepting new tasks and waits for
* all queued tasks to finish before terminating workers.
*/
public void shutdown() {
System.out.println("[ThreadPool] Shutdown initiated...");
isShutdown = true;
// Signal all workers to stop after draining the queue
for (WorkerThread worker : workers) {
worker.shutdown();
}
// Wait for all workers to finish
for (WorkerThread worker : workers) {
try {
worker.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.println("[ThreadPool] All workers stopped.");
stats.printReport();
}
/**
* Immediate shutdown — interrupts all workers without draining the queue.
*/
public void shutdownNow() {
System.out.println("[ThreadPool] Forced shutdown initiated...");
isShutdown = true;
for (WorkerThread worker : workers) {
worker.shutdown();
worker.interrupt();
}
stats.printReport();
}
public int getPoolSize() { return poolSize; }
public int getQueueSize() { return taskQueue.size(); }
public boolean isShutdown() { return isShutdown; }
public ThreadPoolStats getStats() { return stats; }
}