dd8b8294fbf0c6c5a5d0068add0db2424569441d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.infrastructure.threading;
23
24 import java.util.concurrent.ThreadFactory;
25 import java.util.concurrent.atomic.AtomicInteger;
26 import lombok.Getter;
27
28 /**
29  * This class provides a thread factory for use by classes that require thread factories to handle concurrent operation.
30  *
31  * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
32  */
33 public class ApplicationThreadFactory implements ThreadFactory {
34     private static final String HYPHEN = "-";
35     private static final String APPLICATION_NAME = "Apex-";
36     private static final AtomicInteger NEXT_POOL_NUMBER = new AtomicInteger();
37     private final ThreadGroup group;
38     private final AtomicInteger nextThreadNumber = new AtomicInteger();
39
40     @Getter
41     private final String name;
42     @Getter
43     private final long stackSize;
44     @Getter
45     private final int threadPriority;
46
47     /**
48      * Instantiates a new application thread factory with a default stack size and normal thread priority.
49      *
50      * @param nameLocal the name local
51      */
52     public ApplicationThreadFactory(final String nameLocal) {
53         this(nameLocal, 0);
54     }
55
56     /**
57      * Instantiates a new application thread factory with a default normal thread priority.
58      *
59      * @param nameLocal the name local
60      * @param stackSize the stack size
61      */
62     public ApplicationThreadFactory(final String nameLocal, final long stackSize) {
63         this(nameLocal, stackSize, Thread.NORM_PRIORITY);
64     }
65
66     /**
67      * Instantiates a new application thread factory with a specified thread priority.
68      *
69      * @param nameLocal the name local
70      * @param stackSize the stack size
71      * @param threadPriority the thread priority
72      */
73     public ApplicationThreadFactory(final String nameLocal, final long stackSize, final int threadPriority) {
74         final SecurityManager s = System.getSecurityManager();
75         group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
76         name = APPLICATION_NAME + nameLocal + HYPHEN + NEXT_POOL_NUMBER.getAndIncrement();
77         this.stackSize = stackSize;
78         this.threadPriority = threadPriority;
79     }
80
81     /**
82      * {@inheritDoc}.
83      */
84     @Override
85     public Thread newThread(final Runnable runnable) {
86         final Thread thisThread;
87         if (stackSize > 0) {
88             thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement(), stackSize);
89         } else {
90             thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement());
91         }
92         if (thisThread.isDaemon()) {
93             thisThread.setDaemon(false);
94         }
95         thisThread.setPriority(threadPriority);
96
97         return thisThread;
98     }
99
100     /**
101      * Stop group threads.
102      */
103     public void stopGroupThreads() {
104         group.interrupt();
105         group.list();
106
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
113     public String toString() {
114         return "ApplicationThreadFactory [nextPoolNumber=" + NEXT_POOL_NUMBER + ",nextThreadNumber=" + nextThreadNumber
115                 + ", name=" + name + ", stackSize=" + stackSize + ", threadPriority=" + threadPriority + "]";
116     }
117 }