Skip to content

Commit 6800c1a

Browse files
committed
add prototype StripedMonitoredQueuedThreadPool
Signed-off-by: Ludovic Orban <[email protected]>
1 parent dc2f3ea commit 6800c1a

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
//
2+
// ========================================================================
3+
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4+
//
5+
// This program and the accompanying materials are made available under the
6+
// terms of the Eclipse Public License v. 2.0 which is available at
7+
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
9+
//
10+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11+
// ========================================================================
12+
//
13+
14+
package org.eclipse.jetty.util.thread;
15+
16+
import org.eclipse.jetty.util.NanoTime;
17+
import org.eclipse.jetty.util.annotation.ManagedAttribute;
18+
import org.eclipse.jetty.util.annotation.ManagedObject;
19+
import org.eclipse.jetty.util.annotation.ManagedOperation;
20+
import org.eclipse.jetty.util.statistic.CounterStatistic;
21+
import org.eclipse.jetty.util.statistic.SampleStatistic;
22+
23+
/**
24+
* <p>A {@link StripedQueuedThreadPool} subclass that monitors its own activity by recording queue and task statistics.</p>
25+
*/
26+
@ManagedObject
27+
public class StripedMonitoredQueuedThreadPool extends StripedQueuedThreadPool
28+
{
29+
private final CounterStatistic queueStats = new CounterStatistic();
30+
private final SampleStatistic queueLatencyStats = new SampleStatistic();
31+
private final SampleStatistic taskLatencyStats = new SampleStatistic();
32+
private final CounterStatistic threadStats = new CounterStatistic();
33+
34+
public StripedMonitoredQueuedThreadPool()
35+
{
36+
this(256);
37+
}
38+
39+
public StripedMonitoredQueuedThreadPool(int maxThreads)
40+
{
41+
this(maxThreads, maxThreads);
42+
}
43+
44+
public StripedMonitoredQueuedThreadPool(int maxThreads, int minThreads)
45+
{
46+
super(maxThreads, minThreads);
47+
installBean(queueStats);
48+
installBean(queueLatencyStats);
49+
installBean(taskLatencyStats);
50+
installBean(threadStats);
51+
}
52+
53+
@Override
54+
public void execute(final Runnable job)
55+
{
56+
queueStats.increment();
57+
long begin = NanoTime.now();
58+
super.execute(new Runnable()
59+
{
60+
@Override
61+
public void run()
62+
{
63+
long queueLatency = NanoTime.since(begin);
64+
queueStats.decrement();
65+
threadStats.increment();
66+
queueLatencyStats.record(queueLatency);
67+
long start = NanoTime.now();
68+
try
69+
{
70+
job.run();
71+
}
72+
finally
73+
{
74+
long taskLatency = NanoTime.since(start);
75+
threadStats.decrement();
76+
taskLatencyStats.record(taskLatency);
77+
}
78+
}
79+
80+
@Override
81+
public String toString()
82+
{
83+
return job.toString();
84+
}
85+
});
86+
}
87+
88+
/**
89+
* Resets the statistics.
90+
*/
91+
@ManagedOperation(value = "resets the statistics", impact = "ACTION")
92+
public void reset()
93+
{
94+
queueStats.reset();
95+
queueLatencyStats.reset();
96+
taskLatencyStats.reset();
97+
threadStats.reset(0);
98+
}
99+
100+
/**
101+
* @return the number of tasks executed
102+
*/
103+
@ManagedAttribute("the number of tasks executed")
104+
public long getTasks()
105+
{
106+
return taskLatencyStats.getCount();
107+
}
108+
109+
/**
110+
* @return the maximum number of busy threads
111+
*/
112+
@ManagedAttribute("the maximum number of busy threads")
113+
public int getMaxBusyThreads()
114+
{
115+
return (int)threadStats.getMax();
116+
}
117+
118+
/**
119+
* @return the maximum task queue size
120+
*/
121+
@ManagedAttribute("the maximum task queue size")
122+
public int getMaxQueueSize()
123+
{
124+
return (int)queueStats.getMax();
125+
}
126+
127+
/**
128+
* @return the average time a task remains in the queue, in nanoseconds
129+
*/
130+
@ManagedAttribute("the average time a task remains in the queue, in nanoseconds")
131+
public long getAverageQueueLatency()
132+
{
133+
return (long)queueLatencyStats.getMean();
134+
}
135+
136+
/**
137+
* @return the maximum time a task remains in the queue, in nanoseconds
138+
*/
139+
@ManagedAttribute("the maximum time a task remains in the queue, in nanoseconds")
140+
public long getMaxQueueLatency()
141+
{
142+
return queueLatencyStats.getMax();
143+
}
144+
145+
/**
146+
* @return the average task execution time, in nanoseconds
147+
*/
148+
@ManagedAttribute("the average task execution time, in nanoseconds")
149+
public long getAverageTaskLatency()
150+
{
151+
return (long)taskLatencyStats.getMean();
152+
}
153+
154+
/**
155+
* @return the maximum task execution time, in nanoseconds
156+
*/
157+
@ManagedAttribute("the maximum task execution time, in nanoseconds")
158+
public long getMaxTaskLatency()
159+
{
160+
return taskLatencyStats.getMax();
161+
}
162+
}

0 commit comments

Comments
 (0)