Debian Codegraph @

src/main/java/com/jcabi/aspects/aj/NamedThreads.java

raw
1 /**
2 * Copyright (c) 2012-2017, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package com.jcabi.aspects.aj;
31
32 import com.jcabi.aspects.version.Version;
33 import com.jcabi.log.Logger;
34 import java.util.concurrent.ThreadFactory;
35
36 /**
37 * Factory of named threads, used in {@link MethodLogger},
38 * {@link MethodCacher}, {@link MethodInterrupter}, etc.
39 *
40 * <p>This custom class is used instead of a default ThreadFactory in order
41 * to name scheduled threads correctly on construction.
42 *
43 * @author Yegor Bugayenko ([email protected])
44 * @version $Id: e215b72234cd0fff28d27f328e4ced958ae55595 $
45 * @since 0.7.17
46 */
47 @SuppressWarnings("PMD.DoNotUseThreads")
48 final class NamedThreads implements ThreadFactory {
49
50 /**
51 * Name of the thread.
52 */
53 private final transient String name;
54
55 /**
56 * Purpose of these threads.
57 */
58 private final transient String purpose;
59
60 /**
61 * Thread group to use.
62 */
63 private final transient ThreadGroup group;
64
65 /**
66 * Public ctor.
67 * @param suffix Suffix of thread names
68 * @param desc Description of purpose
69 */
70 public NamedThreads(final String suffix, final String desc) {
71 this.name = String.format("jcabi-%s", suffix);
72 this.purpose = desc;
73 this.group = new ThreadGroup("jcabi");
74 }
75
76 @Override
77 public Thread newThread(final Runnable runnable) {
78 final Thread thread = new Thread(this.group, runnable);
79 thread.setName(this.name);
80 thread.setDaemon(true);
81 Logger.info(
82 this,
83 // @checkstyle LineLength (1 line)
84 "jcabi-aspects %s/%s started new daemon thread %s for %s",
85 Version.CURRENT.projectVersion(),
86 Version.CURRENT.buildNumber(),
87 this.name,
88 this.purpose
89 );
90 return thread;
91 }
92
93 }