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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.core.infrastructure.threading;
25 import java.util.concurrent.ThreadFactory;
26 import java.util.concurrent.atomic.AtomicInteger;
30 * This class provides a thread factory for use by classes that require thread factories to handle concurrent operation.
32 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
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();
42 private final String name;
44 private final long stackSize;
46 private final int threadPriority;
49 * Instantiates a new application thread factory with a default stack size and normal thread priority.
51 * @param nameLocal the name local
53 public ApplicationThreadFactory(final String nameLocal) {
58 * Instantiates a new application thread factory with a default normal thread priority.
60 * @param nameLocal the name local
61 * @param stackSize the stack size
63 public ApplicationThreadFactory(final String nameLocal, final long stackSize) {
64 this(nameLocal, stackSize, Thread.NORM_PRIORITY);
68 * Instantiates a new application thread factory with a specified thread priority.
70 * @param nameLocal the name local
71 * @param stackSize the stack size
72 * @param threadPriority the thread priority
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;
86 public Thread newThread(final Runnable runnable) {
87 final Thread thisThread;
89 thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement(), stackSize);
91 thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement());
93 if (thisThread.isDaemon()) {
94 thisThread.setDaemon(false);
96 thisThread.setPriority(threadPriority);
102 * Stop group threads.
104 public void stopGroupThreads() {
114 public String toString() {
115 return "ApplicationThreadFactory [nextPoolNumber=" + NEXT_POOL_NUMBER + ",nextThreadNumber=" + nextThreadNumber
116 + ", name=" + name + ", stackSize=" + stackSize + ", threadPriority=" + threadPriority + "]";