Debian Codegraph @

src/org/apache/commons/lang3/concurrent/BasicThreadFactory.java

raw symbols SCIP
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.lang3.concurrent;
18
19 import java.lang.Thread.UncaughtExceptionHandler;
20 import java.util.Objects;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ThreadFactory;
24 import java.util.concurrent.atomic.AtomicLong;
25
26 /**
27 * An implementation of the {@link ThreadFactory} interface that provides some
28 * configuration options for the threads it creates.
29 * <p>
30 * A {@link ThreadFactory} is used for instance by an {@link ExecutorService} to
31 * create the threads it uses for executing tasks. In many cases users do not
32 * have to care about a {@link ThreadFactory} because the default one used by an
33 * {@link ExecutorService} will do. However, if there are special requirements
34 * for the threads, a custom {@link ThreadFactory} has to be created.
35 * </p>
36 * <p>
37 * This class provides some frequently needed configuration options for the
38 * threads it creates. These are the following:
39 * </p>
40 * <ul>
41 * <li>A name pattern for the threads created by this factory can be specified.
42 * This is often useful if an application uses multiple executor services for
43 * different purposes. If the names of the threads used by these services have
44 * meaningful names, log output or exception traces can be much easier to read.
45 * Naming patterns are <em>format strings</em> as used by the {@code
46 * String.format()} method. The string can contain the place holder {@code %d}
47 * which will be replaced by the number of the current thread ({@code
48 * ThreadFactoryImpl} keeps a counter of the threads it has already created).
49 * For instance, the naming pattern {@code "My %d. worker thread"} will result
50 * in thread names like {@code "My 1. worker thread"}, {@code
51 * "My 2. worker thread"} and so on.</li>
52 * <li>A flag whether the threads created by this factory should be daemon
53 * threads. This can impact the exit behavior of the current Java application
54 * because the JVM shuts down if there are only daemon threads running.</li>
55 * <li>The priority of the thread. Here an integer value can be provided. The
56 * {@link Thread} class defines constants for valid ranges of priority
57 * values.</li>
58 * <li>The {@link UncaughtExceptionHandler} for the thread. This handler is
59 * called if an uncaught exception occurs within the thread.</li>
60 * </ul>
61 * <p>
62 * {@link BasicThreadFactory} wraps another thread factory which actually
63 * creates new threads. The configuration options are set on the threads created
64 * by the wrapped thread factory. On construction time the factory to be wrapped
65 * can be specified. If none is provided, a default {@link ThreadFactory} is
66 * used.
67 * </p>
68 * <p>
69 * Instances of {@link BasicThreadFactory} are not created directly, but the
70 * nested {@link Builder} class is used for this purpose. Using the builder only
71 * the configuration options an application is interested in need to be set. The
72 * following example shows how a {@link BasicThreadFactory} is created and
73 * installed in an {@link ExecutorService}:
74 * </p>
75 *
76 * <pre>
77 * // Create a factory that produces daemon threads with a naming pattern and
78 * // a priority
79 * BasicThreadFactory factory = new BasicThreadFactory.Builder()
80 * .namingPattern(&quot;workerthread-%d&quot;)
81 * .daemon(true)
82 * .priority(Thread.MAX_PRIORITY)
83 * .build();
84 * // Create an executor service for single-threaded execution
85 * ExecutorService exec = Executors.newSingleThreadExecutor(factory);
86 * </pre>
87 *
88 * @since 3.0
89 */
90 public class BasicThreadFactory implements ThreadFactory {
91
92 /**
93 * A <em>builder</em> class for creating instances of {@code
94 * BasicThreadFactory}.
95 * <p>
96 * Using this builder class instances of {@link BasicThreadFactory} can be
97 * created and initialized. The class provides methods that correspond to
98 * the configuration options supported by {@link BasicThreadFactory}. Method
99 * chaining is supported. Refer to the documentation of {@code
100 * BasicThreadFactory} for a usage example.
101 * </p>
102 */
103 public static class Builder implements org.apache.commons.lang3.builder.Builder<BasicThreadFactory> {
104
105 /** The wrapped factory. */
106 private ThreadFactory factory;
107
108 /** The uncaught exception handler. */
109 private Thread.UncaughtExceptionHandler exceptionHandler;
110
111 /** The naming pattern. */
112 private String namingPattern;
113
114 /** The priority. */
115 private Integer priority;
116
117 /** The daemon flag. */
118 private Boolean daemon;
119
120 /**
121 * Constructs a new instance.
122 */
123 public Builder() {
124 // empty
125 }
126
127 /**
128 * Creates a new {@link BasicThreadFactory} with all configuration
129 * options that have been specified by calling methods on this builder.
130 * After creating the factory {@link #reset()} is called.
131 *
132 * @return the new {@link BasicThreadFactory}
133 */
134 @Override
135 public BasicThreadFactory build() {
136 final BasicThreadFactory factory = new BasicThreadFactory(this);
137 reset();
138 return factory;
139 }
140
141 /**
142 * Sets the daemon flag for the new {@link BasicThreadFactory}. If this
143 * flag is set to <b>true</b> the new thread factory will create daemon
144 * threads.
145 *
146 * @param daemon the value of the daemon flag
147 * @return a reference to this {@link Builder}
148 */
149 public Builder daemon(final boolean daemon) {
150 this.daemon = Boolean.valueOf(daemon);
151 return this;
152 }
153
154 /**
155 * Sets the naming pattern to be used by the new {@code
156 * BasicThreadFactory}.
157 *
158 * @param namingPattern the naming pattern (must not be <b>null</b>)
159 * @return a reference to this {@link Builder}
160 * @throws NullPointerException if the naming pattern is <b>null</b>
161 */
162 public Builder namingPattern(final String namingPattern) {
163 this.namingPattern = Objects.requireNonNull(namingPattern, "pattern");
164 return this;
165 }
166
167 /**
168 * Sets the priority for the threads created by the new {@code
169 * BasicThreadFactory}.
170 *
171 * @param priority the priority
172 * @return a reference to this {@link Builder}
173 */
174 public Builder priority(final int priority) {
175 this.priority = Integer.valueOf(priority);
176 return this;
177 }
178
179 /**
180 * Resets this builder. All configuration options are set to default
181 * values. Note: If the {@link #build()} method was called, it is not
182 * necessary to call {@code reset()} explicitly because this is done
183 * automatically.
184 */
185 public void reset() {
186 factory = null;
187 exceptionHandler = null;
188 namingPattern = null;
189 priority = null;
190 daemon = null;
191 }
192
193 /**
194 * Sets the uncaught exception handler for the threads created by the
195 * new {@link BasicThreadFactory}.
196 *
197 * @param exceptionHandler the {@link UncaughtExceptionHandler} (must not be
198 * <b>null</b>)
199 * @return a reference to this {@link Builder}
200 * @throws NullPointerException if the exception handler is <b>null</b>
201 */
202 public Builder uncaughtExceptionHandler(
203 final Thread.UncaughtExceptionHandler exceptionHandler) {
204 this.exceptionHandler = Objects.requireNonNull(exceptionHandler, "handler");
205 return this;
206 }
207
208 /**
209 * Sets the {@link ThreadFactory} to be wrapped by the new {@code
210 * BasicThreadFactory}.
211 *
212 * @param factory the wrapped {@link ThreadFactory} (must not be
213 * <b>null</b>)
214 * @return a reference to this {@link Builder}
215 * @throws NullPointerException if the passed in {@link ThreadFactory}
216 * is <b>null</b>
217 */
218 public Builder wrappedFactory(final ThreadFactory factory) {
219 this.factory = Objects.requireNonNull(factory, "factory");
220 return this;
221 }
222 }
223
224 /** A counter for the threads created by this factory. */
225 private final AtomicLong threadCounter;
226
227 /** Stores the wrapped factory. */
228 private final ThreadFactory wrappedFactory;
229
230 /** Stores the uncaught exception handler. */
231 private final Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
232
233 /** Stores the naming pattern for newly created threads. */
234 private final String namingPattern;
235
236 /** Stores the priority. */
237 private final Integer priority;
238
239 /** Stores the daemon status flag. */
240 private final Boolean daemon;
241
242 /**
243 * Creates a new instance of {@link ThreadFactory} and configures it
244 * from the specified {@link Builder} object.
245 *
246 * @param builder the {@link Builder} object
247 */
248 private BasicThreadFactory(final Builder builder) {
249 wrappedFactory = builder.factory != null ? builder.factory : Executors.defaultThreadFactory();
250 namingPattern = builder.namingPattern;
251 priority = builder.priority;
252 daemon = builder.daemon;
253 uncaughtExceptionHandler = builder.exceptionHandler;
254 threadCounter = new AtomicLong();
255 }
256
257 /**
258 * Gets the daemon flag. This flag determines whether newly created
259 * threads should be daemon threads. If <b>true</b>, this factory object
260 * calls {@code setDaemon(true)} on the newly created threads. Result can be
261 * <b>null</b> if no daemon flag was provided at creation time.
262 *
263 * @return the daemon flag
264 */
265 public final Boolean getDaemonFlag() {
266 return daemon;
267 }
268
269 /**
270 * Gets the naming pattern for naming newly created threads. Result can
271 * be <b>null</b> if no naming pattern was provided.
272 *
273 * @return the naming pattern
274 */
275 public final String getNamingPattern() {
276 return namingPattern;
277 }
278
279 /**
280 * Gets the priority of the threads created by this factory. Result can
281 * be <b>null</b> if no priority was specified.
282 *
283 * @return the priority for newly created threads
284 */
285 public final Integer getPriority() {
286 return priority;
287 }
288
289 /**
290 * Gets the number of threads this factory has already created. This
291 * class maintains an internal counter that is incremented each time the
292 * {@link #newThread(Runnable)} method is invoked.
293 *
294 * @return the number of threads created by this factory
295 */
296 public long getThreadCount() {
297 return threadCounter.get();
298 }
299
300 /**
301 * Gets the {@link UncaughtExceptionHandler} for the threads created by
302 * this factory. Result can be <b>null</b> if no handler was provided.
303 *
304 * @return the {@link UncaughtExceptionHandler}
305 */
306 public final Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() {
307 return uncaughtExceptionHandler;
308 }
309
310 /**
311 * Gets the wrapped {@link ThreadFactory}. This factory is used for
312 * actually creating threads. This method never returns <b>null</b>. If no
313 * {@link ThreadFactory} was passed when this object was created, a default
314 * thread factory is returned.
315 *
316 * @return the wrapped {@link ThreadFactory}
317 */
318 public final ThreadFactory getWrappedFactory() {
319 return wrappedFactory;
320 }
321
322 /**
323 * Initializes the specified thread. This method is called by
324 * {@link #newThread(Runnable)} after a new thread has been obtained from
325 * the wrapped thread factory. It initializes the thread according to the
326 * options set for this factory.
327 *
328 * @param thread the thread to be initialized
329 */
330 private void initializeThread(final Thread thread) {
331 if (getNamingPattern() != null) {
332 final Long count = Long.valueOf(threadCounter.incrementAndGet());
333 thread.setName(String.format(getNamingPattern(), count));
334 }
335 if (getUncaughtExceptionHandler() != null) {
336 thread.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
337 }
338 if (getPriority() != null) {
339 thread.setPriority(getPriority().intValue());
340 }
341 if (getDaemonFlag() != null) {
342 thread.setDaemon(getDaemonFlag().booleanValue());
343 }
344 }
345
346 /**
347 * Creates a new thread. This implementation delegates to the wrapped
348 * factory for creating the thread. Then, on the newly created thread the
349 * corresponding configuration options are set.
350 *
351 * @param runnable the {@link Runnable} to be executed by the new thread
352 * @return the newly created thread
353 */
354 @Override
355 public Thread newThread(final Runnable runnable) {
356 final Thread thread = getWrappedFactory().newThread(runnable);
357 initializeThread(thread);
358 return thread;
359 }
360 }