View Javadoc
1   /*
2    * Copyright (c) 2012-2024, 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   * @since 0.7.17
44   */
45  @SuppressWarnings("PMD.DoNotUseThreads")
46  final class NamedThreads implements ThreadFactory {
47  
48      /**
49       * Name of the thread.
50       */
51      private final transient String name;
52  
53      /**
54       * Purpose of these threads.
55       */
56      private final transient String purpose;
57  
58      /**
59       * Thread group to use.
60       */
61      private final transient ThreadGroup group;
62  
63      /**
64       * Public ctor.
65       * @param suffix Suffix of thread names
66       * @param desc Description of purpose
67       */
68      @SuppressWarnings("PMD.AvoidThreadGroup")
69      NamedThreads(final String suffix, final String desc) {
70          this.name = String.format("jcabi-%s", suffix);
71          this.purpose = desc;
72          this.group = new ThreadGroup("jcabi");
73      }
74  
75      @Override
76      public Thread newThread(final Runnable runnable) {
77          final Thread thread = new Thread(this.group, runnable);
78          thread.setName(this.name);
79          thread.setDaemon(true);
80          Logger.info(
81              this,
82              // @checkstyle LineLength (1 line)
83              "jcabi-aspects %s/%s started new daemon thread %s for %s",
84              Version.CURRENT.projectVersion(),
85              Version.CURRENT.buildNumber(),
86              this.name,
87              this.purpose
88          );
89          return thread;
90      }
91  
92  }