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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.infrastructure.threading;
24 import java.util.concurrent.ThreadFactory;
25 import java.util.concurrent.atomic.AtomicInteger;
29 * This class provides a thread factory for use by classes that require thread factories to handle concurrent operation.
31 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
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();
41 private final String name;
43 private final long stackSize;
45 private final int threadPriority;
48 * Instantiates a new application thread factory with a default stack size and normal thread priority.
50 * @param nameLocal the name local
52 public ApplicationThreadFactory(final String nameLocal) {
57 * Instantiates a new application thread factory with a default normal thread priority.
59 * @param nameLocal the name local
60 * @param stackSize the stack size
62 public ApplicationThreadFactory(final String nameLocal, final long stackSize) {
63 this(nameLocal, stackSize, Thread.NORM_PRIORITY);
67 * Instantiates a new application thread factory with a specified thread priority.
69 * @param nameLocal the name local
70 * @param stackSize the stack size
71 * @param threadPriority the thread priority
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;
85 public Thread newThread(final Runnable runnable) {
86 final Thread thisThread;
88 thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement(), stackSize);
90 thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement());
92 if (thisThread.isDaemon()) {
93 thisThread.setDaemon(false);
95 thisThread.setPriority(threadPriority);
101 * Stop group threads.
103 public void stopGroupThreads() {
113 public String toString() {
114 return "ApplicationThreadFactory [nextPoolNumber=" + NEXT_POOL_NUMBER + ",nextThreadNumber=" + nextThreadNumber
115 + ", name=" + name + ", stackSize=" + stackSize + ", threadPriority=" + threadPriority + "]";