Class ThreadTools


  • public final class ThreadTools
    extends Object
    Thread-related tool methods.
    Author:
    Patrick Schmidt, Ulrich Kreher
    • Method Detail

      • getThreads

        public static Thread[] getThreads​(ThreadGroup group,
                                          Logger logger)
        Gets all threads of the designated thread group. Note that the array may contain null elements and that the threads may already be terminated until they are accessed.
        Parameters:
        group - The thread group of which to recursively retrieve the threads.
        logger - The logger in which to log the log messages.
        Returns:
        The threads of the designated thread group or its sub-groups.
      • dumpAllThreads

        public static void dumpAllThreads​(PrintStream out)
        Dumps stack traces of all current threads to out.
        Parameters:
        out - Target to print the stack dumps to.
      • getCurrentStackTraces

        public static String getCurrentStackTraces​(ThreadGroup group,
                                                   Logger logger)
        Gets the stack traces of all the threads in the designated thread group as string formatted like the usual string representation of a stacktrace.
        Parameters:
        group - The thread group of which to retrieve the stack traces of all threads.
        logger - The logger in which to log the log messages.
        Returns:
        A string representations of all stack traces of the threads of the designated thread group. Each stack trace is prepended by the name of the corresponding thread.
      • destroyThreadGroup

        public static boolean destroyThreadGroup​(ThreadGroup group,
                                                 long joinTimeout,
                                                 Logger logger,
                                                 Level logLevel)
        Destroys the designated thread group if desired ( joinTimeout >= 0). First all active threads are interrupted. Then their termination is awaited with the designated timeout. Threads still running after that will be stopped . Their termination will be awaited again. If all threads terminated in time, the designated thread group will be destroyed.
        If joinTimeout < 0, the thread group will only be destroyed if all its threads have terminated. Otherwise the thread group will not be destroyed and the threads will keep on running.
        Parameters:
        group - The thread group to be destroyed. If this is null , the method call will be ignored.
        joinTimeout - The time in milliseconds to wait for each thread after interrupting and again after stopping it (if required). If this value is negative, the group will only be destroyed if all threads have terminated. Otherwise they will keep on running.
        logger - The logger in which to log the log messages.
        logLevel - The log level which to use for logging running threads. If their termination is forced, this is always Level.SEVERE.
        Returns:
        Whether all threads in the thread group have terminated.