0d5f30737f87517943aae6538a5afcf3d34f2876
[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      * (non-Javadoc)
77      *
78      * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
79      */
80     @Override
81     public Thread newThread(final Runnable runnable) {
82         final Thread thisThread;
83         if (stackSize > 0) {
84             thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement(), stackSize);
85         } else {
86             thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement());
87         }
88         if (thisThread.isDaemon()) {
89             thisThread.setDaemon(false);
90         }
91         thisThread.setPriority(threadPriority);
92
93         return thisThread;
94     }
95
96     /**
97      * Stop group threads.
98      */
99     public void stopGroupThreads() {
100         group.interrupt();
101         group.list();
102
103     }
104
105     /**
106      * Gets the name of the thread factory.
107      *
108      * @return the name
109      */
110     public String getName() {
111         return name;
112     }
113
114     /**
115      * Gets the stack size of the threads created by this thread factory.
116      *
117      * @return the stack size
118      */
119     public long getStackSize() {
120         return stackSize;
121     }
122
123     /**
124      * Gets the thread priority of the threads created by this thread factory.
125      *
126      * @return the thread priority
127      */
128     public int getThreadPriority() {
129         return threadPriority;
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see java.lang.Object#toString()
136      */
137     @Override
138     public String toString() {
139         return "ApplicationThreadFactory [nextPollNumber=" + NEXT_POOL_NUMBER + ",nextThreadNumber=" + nextThreadNumber
140                 + ", name=" + name + ", stackSize=" + stackSize + ", threadPriority=" + threadPriority + "]";
141     }
142 }