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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.threading;
23 import java.util.concurrent.ThreadFactory;
24 import java.util.concurrent.atomic.AtomicInteger;
27 * This class provides a thread factory for use by classes that require thread factories to handle concurrent operation.
29 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
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;
42 * Instantiates a new application thread factory with a default stack size and normal thread priority.
44 * @param nameLocal the name local
46 public ApplicationThreadFactory(final String nameLocal) {
51 * Instantiates a new application thread factory with a default normal thread priority.
53 * @param nameLocal the name local
54 * @param stackSize the stack size
56 public ApplicationThreadFactory(final String nameLocal, final long stackSize) {
57 this(nameLocal, stackSize, Thread.NORM_PRIORITY);
61 * Instantiates a new application thread factory with a specified thread priority.
63 * @param nameLocal the name local
64 * @param stackSize the stack size
65 * @param threadPriority the thread priority
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;
78 * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
81 public Thread newThread(final Runnable runnable) {
82 final Thread thisThread;
84 thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement(), stackSize);
86 thisThread = new Thread(group, runnable, name + ':' + nextThreadNumber.getAndIncrement());
88 if (thisThread.isDaemon()) {
89 thisThread.setDaemon(false);
91 thisThread.setPriority(threadPriority);
99 public void stopGroupThreads() {
106 * Gets the name of the thread factory.
110 public String getName() {
115 * Gets the stack size of the threads created by this thread factory.
117 * @return the stack size
119 public long getStackSize() {
124 * Gets the thread priority of the threads created by this thread factory.
126 * @return the thread priority
128 public int getThreadPriority() {
129 return threadPriority;
135 * @see java.lang.Object#toString()
138 public String toString() {
139 return "ApplicationThreadFactory [nextPollNumber=" + NEXT_POOL_NUMBER + ",nextThreadNumber=" + nextThreadNumber
140 + ", name=" + name + ", stackSize=" + stackSize + ", threadPriority=" + threadPriority + "]";