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