dc9c13d7bc677ac1a1625af1f4d5e5c933603793
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.infrastructure.threading;
22
23 import java.util.concurrent.ThreadFactory;
24 import java.util.concurrent.atomic.AtomicInteger;
25
26 /**
27  * This class provides a thread factory for use by classes that require thread factories to handle concurrent operation.
28  *
29  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
30  */
31 public class ApplicationThreadFactory implements ThreadFactory {
32     private static final String HYPHEN = "-";
33     private static final String APPLICATION_NAME = "Apex-";
34     private static final AtomicInteger NEXT_POOL_NUMBER = new AtomicInteger();
35     private final ThreadGroup group;
36     private final AtomicInteger nextThreadNumber = new AtomicInteger();
37     private final String name;
38     private final long stackSize;
39     private final int threadPriority;
40
41     /**
42      * Instantiates a new application thread factory with a default stack size and normal thread priority.
43      *
44      * @param nameLocal the name local
45      */
46     public ApplicationThreadFactory(final String nameLocal) {
47         this(nameLocal, 0);
48     }
49
50     /**
51      * Instantiates a new application thread factory with a default normal thread priority.
52      *
53      * @param nameLocal the name local
54      * @param stackSize the stack size
55      */
56     public ApplicationThreadFactory(final String nameLocal, final long stackSize) {
57         this(nameLocal, stackSize, Thread.NORM_PRIORITY);
58     }
59
60     /**
61      * Instantiates a new application thread factory with a specified thread priority.
62      *
63      * @param nameLocal the name local
64      * @param stackSize the stack size
65      * @param threadPriority the thread priority
66      */
67     public ApplicationThreadFactory(final String nameLocal, final long stackSize, final int threadPriority) {
68         final SecurityManager s = System.getSecurityManager();
69         group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
70         name = APPLICATION_NAME + nameLocal + HYPHEN + NEXT_POOL_NUMBER.getAndIncrement();
71         this.stackSize = stackSize;
72         this.threadPriority = threadPriority;
73     }
74
75     /**
76      * {@inheritDoc}.
77      */
78     @Override
79     public Thread newThread(final Runnable runnable) {
80         final Thread thisThread;
81         if (stackSize > 0) {
82             thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement(), stackSize);
83         } else {
84             thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement());
85         }
86         if (thisThread.isDaemon()) {
87             thisThread.setDaemon(false);
88         }
89         thisThread.setPriority(threadPriority);
90
91         return thisThread;
92     }
93
94     /**
95      * Stop group threads.
96      */
97     public void stopGroupThreads() {
98         group.interrupt();
99         group.list();
100
101     }
102
103     /**
104      * Gets the name of the thread factory.
105      *
106      * @return the name
107      */
108     public String getName() {
109         return name;
110     }
111
112     /**
113      * Gets the stack size of the threads created by this thread factory.
114      *
115      * @return the stack size
116      */
117     public long getStackSize() {
118         return stackSize;
119     }
120
121     /**
122      * Gets the thread priority of the threads created by this thread factory.
123      *
124      * @return the thread priority
125      */
126     public int getThreadPriority() {
127         return threadPriority;
128     }
129
130     /**
131      * {@inheritDoc}.
132      */
133     @Override
134     public String toString() {
135         return "ApplicationThreadFactory [nextPollNumber=" + NEXT_POOL_NUMBER + ",nextThreadNumber=" + nextThreadNumber
136                 + ", name=" + name + ", stackSize=" + stackSize + ", threadPriority=" + threadPriority + "]";
137     }
138 }