-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolStats.java
More file actions
47 lines (38 loc) · 1.7 KB
/
ThreadPoolStats.java
File metadata and controls
47 lines (38 loc) · 1.7 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
package com.taskscheduler;
import java.util.concurrent.atomic.AtomicLong;
/**
* Tracks runtime statistics for the thread pool.
* Thread-safe using atomic operations.
*/
public class ThreadPoolStats {
private final AtomicLong totalCompleted = new AtomicLong(0);
private final AtomicLong totalFailed = new AtomicLong(0);
private final AtomicLong totalWaitTime = new AtomicLong(0);
private final AtomicLong totalExecTime = new AtomicLong(0);
public void recordCompletion(long waitTimeMs, long execTimeMs) {
totalCompleted.incrementAndGet();
totalWaitTime.addAndGet(waitTimeMs);
totalExecTime.addAndGet(execTimeMs);
}
public void recordFailure() {
totalFailed.incrementAndGet();
}
public long getTotalCompleted() { return totalCompleted.get(); }
public long getTotalFailed() { return totalFailed.get(); }
public double getAverageWaitTime() {
long completed = totalCompleted.get();
return completed == 0 ? 0 : (double) totalWaitTime.get() / completed;
}
public double getAverageExecTime() {
long completed = totalCompleted.get();
return completed == 0 ? 0 : (double) totalExecTime.get() / completed;
}
public void printReport() {
System.out.println("\n========== Thread Pool Stats ==========");
System.out.printf(" Tasks completed : %d%n", getTotalCompleted());
System.out.printf(" Tasks failed : %d%n", getTotalFailed());
System.out.printf(" Avg wait time : %.2f ms%n", getAverageWaitTime());
System.out.printf(" Avg exec time : %.2f ms%n", getAverageExecTime());
System.out.println("=======================================\n");
}
}