annotate src/org/eclipse/jetty/util/thread/QueuedThreadPool.java @ 863:88d3c8ff242a

remove SizedThreadPool
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 02 Oct 2016 05:22:55 -0600
parents 8e9db0bbf4f9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 // ------------------------------------------------------------------------
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 // All rights reserved. This program and the accompanying materials
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 // are made available under the terms of the Eclipse Public License v1.0
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 // and Apache License v2.0 which accompanies this distribution.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 // The Eclipse Public License is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 // http://www.eclipse.org/legal/epl-v10.html
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 // The Apache License v2.0 is available at
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 // http://www.opensource.org/licenses/apache2.0.php
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 // You may elect to redistribute this code under either of these licenses.
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 // ========================================================================
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 //
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 package org.eclipse.jetty.util.thread;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 import java.io.IOException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 import java.util.ArrayList;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 import java.util.Arrays;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 import java.util.List;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 import java.util.concurrent.ArrayBlockingQueue;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 import java.util.concurrent.BlockingQueue;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 import java.util.concurrent.ConcurrentLinkedQueue;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 import java.util.concurrent.Executor;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 import java.util.concurrent.RejectedExecutionException;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 import java.util.concurrent.TimeUnit;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 import java.util.concurrent.atomic.AtomicInteger;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 import java.util.concurrent.atomic.AtomicLong;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 import org.eclipse.jetty.util.BlockingArrayQueue;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 import org.eclipse.jetty.util.component.AbstractLifeCycle;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 import org.eclipse.jetty.util.component.AggregateLifeCycle;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 import org.eclipse.jetty.util.component.Dumpable;
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 import org.eclipse.jetty.util.component.LifeCycle;
820
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
40 import org.slf4j.Logger;
8e9db0bbf4f9 remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 802
diff changeset
41 import org.slf4j.LoggerFactory;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
43 public class QueuedThreadPool extends AbstractLifeCycle implements ThreadPool, Executor, Dumpable
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 {
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
45 private static final Logger LOG = LoggerFactory.getLogger(QueuedThreadPool.class);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
47 private final AtomicInteger _threadsStarted = new AtomicInteger();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
48 private final AtomicInteger _threadsIdle = new AtomicInteger();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
49 private final AtomicLong _lastShrink = new AtomicLong();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
50 private final ConcurrentLinkedQueue<Thread> _threads=new ConcurrentLinkedQueue<Thread>();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
51 private final Object _joinLock = new Object();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
52 private BlockingQueue<Runnable> _jobs;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
53 private String _name;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
54 private int _maxIdleTimeMs=60000;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
55 private int _maxThreads=254;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
56 private int _minThreads=8;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
57 private int _maxQueued=-1;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
58 private int _priority=Thread.NORM_PRIORITY;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
59 private boolean _daemon=false;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
60 private int _maxStopTime=100;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
61 private boolean _detailedDump=false;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
63 /* ------------------------------------------------------------------- */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
64 /** Construct
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
65 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
66 public QueuedThreadPool()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
67 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
68 _name="qtp"+super.hashCode();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
69 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
71 /* ------------------------------------------------------------------- */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
72 /** Construct
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
73 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
74 public QueuedThreadPool(int maxThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
75 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
76 this();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
77 setMaxThreads(maxThreads);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
78 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
80 /* ------------------------------------------------------------------- */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
81 /** Construct
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
82 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
83 public QueuedThreadPool(BlockingQueue<Runnable> jobQ)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
84 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
85 this();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
86 _jobs=jobQ;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
87 _jobs.clear();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
88 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
91 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
92 @Override
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
93 protected void doStart() throws Exception
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
94 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
95 super.doStart();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
96 _threadsStarted.set(0);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
98 if (_jobs==null)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
99 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
100 _jobs=_maxQueued>0 ?new ArrayBlockingQueue<Runnable>(_maxQueued)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
101 :new BlockingArrayQueue<Runnable>(_minThreads,_minThreads);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
102 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
104 int threads=_threadsStarted.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
105 while (isRunning() && threads<_minThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
106 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
107 startThread(threads);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
108 threads=_threadsStarted.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
109 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
110 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
112 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
113 @Override
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
114 protected void doStop() throws Exception
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
115 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
116 super.doStop();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
117 long start=System.currentTimeMillis();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
119 // let jobs complete naturally for a while
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
120 while (_threadsStarted.get()>0 && (System.currentTimeMillis()-start) < (_maxStopTime/2))
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
121 Thread.sleep(1);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
123 // kill queued jobs and flush out idle jobs
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
124 _jobs.clear();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
125 Runnable noop = new Runnable(){public void run(){}};
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
126 for (int i=_threadsIdle.get();i-->0;)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
127 _jobs.offer(noop);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
128 Thread.yield();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
130 // interrupt remaining threads
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
131 if (_threadsStarted.get()>0)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
132 for (Thread thread : _threads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
133 thread.interrupt();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
135 // wait for remaining threads to die
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
136 while (_threadsStarted.get()>0 && (System.currentTimeMillis()-start) < _maxStopTime)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
137 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
138 Thread.sleep(1);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
139 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
140 Thread.yield();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
141 int size=_threads.size();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
142 if (size>0)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
143 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
144 LOG.warn(size+" threads could not be stopped");
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
146 if (size==1 || LOG.isDebugEnabled())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
147 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
148 for (Thread unstopped : _threads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
149 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
150 LOG.info("Couldn't stop "+unstopped);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
151 for (StackTraceElement element : unstopped.getStackTrace())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
152 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
153 LOG.info(" at "+element);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
154 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
155 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
156 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
157 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
159 synchronized (_joinLock)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
160 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
161 _joinLock.notifyAll();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
162 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
163 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
165 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
166 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
167 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
168 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
169 public void setDaemon(boolean daemon)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
170 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
171 _daemon=daemon;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
172 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
174 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
175 /** Set the maximum thread idle time.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
176 * Threads that are idle for longer than this period may be
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
177 * stopped.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
178 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
179 * @see #getMaxIdleTimeMs
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
180 * @param maxIdleTimeMs Max idle time in ms.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
181 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
182 public void setMaxIdleTimeMs(int maxIdleTimeMs)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
183 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
184 _maxIdleTimeMs=maxIdleTimeMs;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
185 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
187 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
188 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
189 * @param stopTimeMs maximum total time that stop() will wait for threads to die.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
190 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
191 public void setMaxStopTimeMs(int stopTimeMs)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
192 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
193 _maxStopTime = stopTimeMs;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
194 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
196 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
197 /** Set the maximum number of threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
198 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
199 * @see #getMaxThreads
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
200 * @param maxThreads maximum number of threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
201 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
202 public void setMaxThreads(int maxThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
203 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
204 _maxThreads=maxThreads;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
205 if (_minThreads>_maxThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
206 _minThreads=_maxThreads;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
207 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
209 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
210 /** Set the minimum number of threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
211 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
212 * @see #getMinThreads
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
213 * @param minThreads minimum number of threads
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
214 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
215 public void setMinThreads(int minThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
216 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
217 _minThreads=minThreads;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
218
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
219 if (_minThreads>_maxThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
220 _maxThreads=_minThreads;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
221
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
222 int threads=_threadsStarted.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
223 while (isStarted() && threads<_minThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
224 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
225 startThread(threads);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
226 threads=_threadsStarted.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
227 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
228 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
229
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
230 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
231 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
232 * @param name Name of the BoundedThreadPool to use when naming Threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
233 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
234 public void setName(String name)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
235 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
236 if (isRunning())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
237 throw new IllegalStateException("started");
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
238 _name= name;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
239 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
240
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
241 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
242 /** Set the priority of the pool threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
243 * @param priority the new thread priority.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
244 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
245 public void setThreadsPriority(int priority)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
246 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
247 _priority=priority;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
248 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
249
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
250 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
251 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
252 * @return maximum queue size
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
253 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
254 public int getMaxQueued()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
255 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
256 return _maxQueued;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
257 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
258
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
259 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
260 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
261 * @param max job queue size
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
262 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
263 public void setMaxQueued(int max)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
264 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
265 if (isRunning())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
266 throw new IllegalStateException("started");
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
267 _maxQueued=max;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
268 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
270 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
271 /** Get the maximum thread idle time.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
272 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
273 * @see #setMaxIdleTimeMs
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
274 * @return Max idle time in ms.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
275 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
276 public int getMaxIdleTimeMs()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
277 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
278 return _maxIdleTimeMs;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
279 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
281 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
282 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
283 * @return maximum total time that stop() will wait for threads to die.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
284 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
285 public int getMaxStopTimeMs()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
286 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
287 return _maxStopTime;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
288 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
289
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
290 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
291 /** Set the maximum number of threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
292 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
293 * @see #setMaxThreads
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
294 * @return maximum number of threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
295 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
296 public int getMaxThreads()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
297 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
298 return _maxThreads;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
299 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
300
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
301 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
302 /** Get the minimum number of threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
303 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
304 * @see #setMinThreads
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
305 * @return minimum number of threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
306 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
307 public int getMinThreads()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
308 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
309 return _minThreads;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
310 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
311
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
312 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
313 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
314 * @return The name of the BoundedThreadPool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
315 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
316 public String getName()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
317 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
318 return _name;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
319 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
320
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
321 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
322 /** Get the priority of the pool threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
323 * @return the priority of the pool threads.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
324 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
325 public int getThreadsPriority()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
326 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
327 return _priority;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
328 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
329
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
330 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
331 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
332 * Delegated to the named or anonymous Pool.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
333 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
334 public boolean isDaemon()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
335 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
336 return _daemon;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
337 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
338
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
339 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
340 public boolean isDetailedDump()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
341 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
342 return _detailedDump;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
343 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
344
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
345 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
346 public void setDetailedDump(boolean detailedDump)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
347 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
348 _detailedDump = detailedDump;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
349 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
350
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
351 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
352 public boolean dispatch(Runnable job)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
353 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
354 if (isRunning())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
355 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
356 final int jobQ = _jobs.size();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
357 final int idle = getIdleThreads();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
358 if(_jobs.offer(job))
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
359 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
360 // If we had no idle threads or the jobQ is greater than the idle threads
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
361 if (idle==0 || jobQ>idle)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
362 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
363 int threads=_threadsStarted.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
364 if (threads<_maxThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
365 startThread(threads);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
366 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
367 return true;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
368 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
369 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
370 LOG.debug("Dispatched {} to stopped {}",job,this);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
371 return false;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
372 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
373
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
374 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
375 public void execute(Runnable job)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
376 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
377 if (!dispatch(job))
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
378 throw new RejectedExecutionException();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
379 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
380
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
381 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
382 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
383 * Blocks until the thread pool is {@link LifeCycle#stop stopped}.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
384 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
385 public void join() throws InterruptedException
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
386 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
387 synchronized (_joinLock)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
388 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
389 while (isRunning())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
390 _joinLock.wait();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
391 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
392
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
393 while (isStopping())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
394 Thread.sleep(1);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
395 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
396
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
397 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
398 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
399 * @return The total number of threads currently in the pool
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
400 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
401 public int getThreads()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
402 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
403 return _threadsStarted.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
404 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
405
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
406 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
407 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
408 * @return The number of idle threads in the pool
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
409 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
410 public int getIdleThreads()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
411 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
412 return _threadsIdle.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
413 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
414
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
415 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
416 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
417 * @return True if the pool is at maxThreads and there are not more idle threads than queued jobs
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
418 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
419 public boolean isLowOnThreads()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
420 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
421 return _threadsStarted.get()==_maxThreads && _jobs.size()>=_threadsIdle.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
422 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
423
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
424 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
425 private boolean startThread(int threads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
426 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
427 final int next=threads+1;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
428 if (!_threadsStarted.compareAndSet(threads,next))
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
429 return false;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
430
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
431 boolean started=false;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
432 try
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
433 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
434 Thread thread=newThread(_runnable);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
435 thread.setDaemon(_daemon);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
436 thread.setPriority(_priority);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
437 thread.setName(_name+"-"+thread.getId());
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
438 _threads.add(thread);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
439
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
440 thread.start();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
441 started=true;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
442 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
443 finally
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
444 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
445 if (!started)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
446 _threadsStarted.decrementAndGet();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
447 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
448 return started;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
449 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
450
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
451 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
452 protected Thread newThread(Runnable runnable)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
453 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
454 return new Thread(runnable);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
455 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
456
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
457
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
458 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
459 public String dump()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
460 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
461 return AggregateLifeCycle.dump(this);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
462 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
463
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
464 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
465 public void dump(Appendable out, String indent) throws IOException
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
466 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
467 List<Object> dump = new ArrayList<Object>(getMaxThreads());
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
468 for (final Thread thread: _threads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
469 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
470 final StackTraceElement[] trace=thread.getStackTrace();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
471 boolean inIdleJobPoll=false;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
472 // trace can be null on early java 6 jvms
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
473 if (trace != null)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
474 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
475 for (StackTraceElement t : trace)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
476 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
477 if ("idleJobPoll".equals(t.getMethodName()))
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
478 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
479 inIdleJobPoll = true;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
480 break;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
481 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
482 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
483 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
484 final boolean idle=inIdleJobPoll;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
485
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
486 if (_detailedDump)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
487 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
488 dump.add(new Dumpable()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
489 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
490 public void dump(Appendable out, String indent) throws IOException
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
491 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
492 out.append(String.valueOf(thread.getId())).append(' ').append(thread.getName()).append(' ').append(thread.getState().toString()).append(idle?" IDLE":"").append('\n');
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
493 if (!idle)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
494 AggregateLifeCycle.dump(out,indent,Arrays.asList(trace));
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
495 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
496
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
497 public String dump()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
498 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
499 return null;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
500 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
501 });
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
502 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
503 else
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
504 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
505 dump.add(thread.getId()+" "+thread.getName()+" "+thread.getState()+" @ "+(trace.length>0?trace[0]:"???")+(idle?" IDLE":""));
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
506 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
507 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
508
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
509 AggregateLifeCycle.dumpObject(out,this);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
510 AggregateLifeCycle.dump(out,indent,dump);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
511
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
512 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
513
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
514
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
515 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
516 @Override
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
517 public String toString()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
518 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
519 return _name+"{"+getMinThreads()+"<="+getIdleThreads()+"<="+getThreads()+"/"+getMaxThreads()+","+(_jobs==null?-1:_jobs.size())+"}";
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
520 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
521
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
522 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
523 private Runnable idleJobPoll() throws InterruptedException
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
524 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
525 return _jobs.poll(_maxIdleTimeMs,TimeUnit.MILLISECONDS);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
526 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
527
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
528 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
529 private Runnable _runnable = new Runnable()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
530 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
531 public void run()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
532 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
533 boolean shrink=false;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
534 try
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
535 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
536 Runnable job=_jobs.poll();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
537 while (isRunning())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
538 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
539 // Job loop
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
540 while (job!=null && isRunning())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
541 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
542 runJob(job);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
543 job=_jobs.poll();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
544 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
545
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
546 // Idle loop
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
547 try
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
548 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
549 _threadsIdle.incrementAndGet();
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
550
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
551 while (isRunning() && job==null)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
552 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
553 if (_maxIdleTimeMs<=0)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
554 job=_jobs.take();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
555 else
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
556 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
557 // maybe we should shrink?
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
558 final int size=_threadsStarted.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
559 if (size>_minThreads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
560 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
561 long last=_lastShrink.get();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
562 long now=System.currentTimeMillis();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
563 if (last==0 || (now-last)>_maxIdleTimeMs)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
564 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
565 shrink=_lastShrink.compareAndSet(last,now) &&
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
566 _threadsStarted.compareAndSet(size,size-1);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
567 if (shrink)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
568 return;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
569 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
570 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
571 job=idleJobPoll();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
572 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
573 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
574 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
575 finally
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
576 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
577 _threadsIdle.decrementAndGet();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
578 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
579 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
580 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
581 catch(InterruptedException e)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
582 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
583 LOG.trace("",e);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
584 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
585 catch(Exception e)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
586 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
587 LOG.warn("",e);
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
588 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
589 finally
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
590 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
591 if (!shrink)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
592 _threadsStarted.decrementAndGet();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
593 _threads.remove(Thread.currentThread());
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
594 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
595 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
596 };
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
597
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
598 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
599 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
600 * <p>Runs the given job in the {@link Thread#currentThread() current thread}.</p>
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
601 * <p>Subclasses may override to perform pre/post actions before/after the job is run.</p>
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
602 *
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
603 * @param job the job to run
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
604 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
605 protected void runJob(Runnable job)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
606 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
607 job.run();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
608 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
609
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
610 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
611 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
612 * @return the job queue
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
613 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
614 protected BlockingQueue<Runnable> getQueue()
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
615 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
616 return _jobs;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
617 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
618
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
619 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
620 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
621 * @param id The thread ID to stop.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
622 * @return true if the thread was found and stopped.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
623 * @deprecated Use {@link #interruptThread(long)} in preference
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
624 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
625 @Deprecated
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
626 public boolean stopThread(long id)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
627 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
628 for (Thread thread: _threads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
629 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
630 if (thread.getId()==id)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
631 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
632 thread.stop();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
633 return true;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
634 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
635 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
636 return false;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
637 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
638
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
639 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
640 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
641 * @param id The thread ID to interrupt.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
642 * @return true if the thread was found and interrupted.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
643 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
644 public boolean interruptThread(long id)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
645 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
646 for (Thread thread: _threads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
647 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
648 if (thread.getId()==id)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
649 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
650 thread.interrupt();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
651 return true;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
652 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
653 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
654 return false;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
655 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
656
863
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
657 /* ------------------------------------------------------------ */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
658 /**
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
659 * @param id The thread ID to interrupt.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
660 * @return true if the thread was found and interrupted.
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
661 */
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
662 public String dumpThread(long id)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
663 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
664 for (Thread thread: _threads)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
665 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
666 if (thread.getId()==id)
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
667 {
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
668 StringBuilder buf = new StringBuilder();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
669 buf.append(thread.getId()).append(" ").append(thread.getName()).append(" ").append(thread.getState()).append(":\n");
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
670 for (StackTraceElement element : thread.getStackTrace())
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
671 buf.append(" at ").append(element.toString()).append('\n');
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
672 return buf.toString();
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
673 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
674 }
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
675 return null;
88d3c8ff242a remove SizedThreadPool
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
676 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
677 }