From 6ec6789954a0d0671b93bcd5f44b915fe7258cd3 Mon Sep 17 00:00:00 2001 From: Tony Hansen Date: Wed, 23 Aug 2017 18:54:59 +0000 Subject: Remove ThreadCommon.java file from dcae-pgaas Change-Id: I0a1279976f25447dfab63e73bd6e1b3974fb2487 Signed-off-by: Tony Hansen Issue: DCAEGEN2-77 --- .../cdf-prop-value/cdf-util/src/main/java/makefile | 2 +- .../dcae/cdf/util/threads/ThreadCommon.java | 119 --------------------- .../dcae/cdf/util/threads/ThreadUtils.java | 89 +++++++++++++++ 3 files changed, 90 insertions(+), 120 deletions(-) delete mode 100644 cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadCommon.java create mode 100644 cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadUtils.java diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/makefile b/cdf/src/cdf-prop-value/cdf-util/src/main/java/makefile index af2f0b0..9f04d67 100644 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/makefile +++ b/cdf/src/cdf-prop-value/cdf-util/src/main/java/makefile @@ -22,7 +22,7 @@ JAVA= org/openecomp/dcae/cdf/util/common/Classify.java \ org/openecomp/dcae/cdf/util/config/Configurable.java \ org/openecomp/dcae/cdf/util/config/ConfigurationRegistry.java \ org/openecomp/dcae/cdf/util/threads/TaskThread.java \ - org/openecomp/dcae/cdf/util/threads/ThreadCommon.java + org/openecomp/dcae/cdf/util/threads/ThreadUtils.java CLASSES=$(JAVA:.java=.class) diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadCommon.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadCommon.java deleted file mode 100644 index fd9f3c1..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadCommon.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code except in compliance - with the License. You may obtain a copy of the License - at http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied. See the License for the specific language governing - permissions and limitations under the License. - -*/ -package org.openecomp.dcae.cdf.util.threads; - -/** - * Some functions to manipulate thread info. Based on - * http://nadeausoftware.com/articles/2008/04/java_tip_how_list_and_find_threads_and_thread_groups - * which is licensed under LGPL 2 - */ - -import java.lang.management.*; -// import java.util.Arrays; - -public final class ThreadCommon { - /** - * ThreadCommon is not to be instantiated. - */ - private ThreadCommon() { } - - private static ThreadGroup rootThreadGroup = null; - - /** - * Get the root thread group in the thread group tree. - * Since there is always a root thread group, this - * method never returns null. - * - * @return the root thread group - */ - public static synchronized ThreadGroup getRootThreadGroup() { - if ( rootThreadGroup != null ) - return rootThreadGroup; - ThreadGroup tg = Thread.currentThread().getThreadGroup(); - ThreadGroup ptg; - while ( (ptg = tg.getParent()) != null ) - tg = ptg; - return tg; - } - - /** - * Get a list of all threads. Since there is always at - * least one thread, this method never returns null or - * an empty array. - * - * @return an array of threads - */ - public static Thread[] getAllThreads() { - final ThreadGroup root = getRootThreadGroup(); - final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); - int nAlloc = thbean.getThreadCount(); - int n = 0; - Thread[] threads; - do { - nAlloc *= 2; - threads = new Thread[ nAlloc ]; - n = root.enumerate( threads, true ); - } while ( n == nAlloc ); - return copyOf( threads, n ); - } - - /** - * Get the thread with the given name. A null is returned - * if no such thread is found. If more than one thread has - * the same name, the first one found is returned. - * - * @param name the thread name to search for - * @return the thread, or null if not found - * @throws NullPointerException - * if the name is null - */ - public static Thread getFirstThread( final String name ) { - if ( name == null ) - throw new NullPointerException( "Null name" ); - final Thread[] threads = getAllThreads(); - for ( Thread thread : threads ) - if ( thread.getName().equals( name ) ) - return thread; - return null; - } - - /** - * Get a list of all threads with a given thread name. - * - * @param name the name to look for - * @return an array of threads in that state - */ - public static Thread[] getAllThreads( final String name ) { - if ( name == null ) - throw new NullPointerException( "Null name" ); - final Thread[] allThreads = getAllThreads(); - final Thread[] found = new Thread[allThreads.length]; - int nFound = 0; - for ( Thread thread : allThreads ) - if ( thread.getName().equals(name) ) - found[nFound++] = thread; - return copyOf( found, nFound ); - } - - // return java.util.Arrays.copyOf( found, nFound ); - private static Thread[] copyOf( Thread[] threads, int n ) { - Thread[] nthreads = new Thread[ n ]; - for (int i = 0; i < n; i++) { - nthreads[i] = threads[i]; - } - return nthreads; - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadUtils.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadUtils.java new file mode 100644 index 0000000..34afebe --- /dev/null +++ b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/openecomp/dcae/cdf/util/threads/ThreadUtils.java @@ -0,0 +1,89 @@ +/* + Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this code except in compliance + with the License. You may obtain a copy of the License + at http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied. See the License for the specific language governing + permissions and limitations under the License. + +*/ +package org.onap.dcae.cdf.util.threads; + +import java.util.LinkedList; +import java.util.List; + +/** + * Various utility functions dealing with threads + */ +public class ThreadUtils { + /** + * Get a list of all threads. + * + * @return an array of threads + */ + public static Thread[] getAllThreads() { + ThreadGroup rootGroup = getRootGroup(); + int noThreads = rootGroup.activeCount(); // returns an estimated count of active threads + Thread[] threads = new Thread[noThreads + 1]; + rootGroup.enumerate(threads); + + while ( rootGroup.enumerate( threads, true ) == threads.length ) { // iterate if we filled up the array + threads = new Thread[ threads.length + noThreads ]; + } + // remove null threads + LinkedList lthreads = new LinkedList(); + for (Thread thread: threads) + if (thread != null) + lthreads.push(thread); + return lthreads.toArray(new Thread[0]); + } + + /** + * Get a list of all threads with a given thread name. + * + * @param name the name to look for + * @return an array of threads with that name + */ + public static Thread[] getNamedThreads(final String name) { + Thread[] allThreads = getAllThreads(); + LinkedList lthreads = new LinkedList(); + for (Thread thread: allThreads) + if (thread.getName().equals(name)) + lthreads.push(thread); + + return lthreads.toArray(new Thread[0]); + } + + /** + * Get the ultimate root of the threads + * + * @return the root thread + */ + public static ThreadGroup getRootGroup() { + ThreadGroup rootGroup = Thread.currentThread( ).getThreadGroup( ); + ThreadGroup parentGroup; + while ( ( parentGroup = rootGroup.getParent() ) != null ) { + rootGroup = parentGroup; + } + return rootGroup; + } + + public static void main(String args[]) throws Exception { + System.out.println("==== get Root Threads ===="); + System.out.println("Root thread = " + getRootGroup().getName()); + System.out.println("==== get All Threads ===="); + Thread[] threads = getAllThreads(); + for (int i = 0; i < threads.length; i++) + System.out.println("Thread No:" + i + " = " + threads[i].getName()); + System.out.println("==== getNamedThreads(main) ===="); + threads = getNamedThreads("main"); + for (int i = 0; i < threads.length; i++) + System.out.println("Thread No:" + i + " = " + threads[i].getName()); + } +} -- cgit 1.2.3-korg