/** * Tries to finish all running user threads. JVM threads are filtered and will be ignored. Uses the default maximal * time to spend on joining threads {@link #THREAD_WAIT_TIMEOUT} (can apply two times!). * * @param dryRun * If <code>true</code> this method does not really interrupt threads. * @param threadSnapshot * Threads recorded by a thread snapshot are ignored by this method. * @return Returns <code>true</code> if on of the threads could not be finished using an interrupt. <code>false</code> * is returned if all threads terminated cleanly. * */ public static boolean finishRunningThreads(boolean dryRun, ThreadSnapshot threadSnapshot) { return finishRunningThreads(dryRun, THREAD_WAIT_TIMEOUT, threadSnapshot); }
/** * Tries to find a thread with the specified id. * * @param id * The thread id from a {@link ThreadSnapshot} object. * @return Returns the thread if it is currently active/known by the JVM, or <code>null</code> if the thread has been * terminated. */ public static Thread getThread(Integer id) { Set<Thread> allThreads = ThreadCleaning.getAllThreads(); for (Thread t : allThreads) { if (System.identityHashCode(t) == id) { return t; } } return null; } }
/** * This method checks if a thread should be processed by thread cleaning features. * * @param thread * The thread to check * @param threadSnapshot * Threads recorded by a thread snapshot are ignored by this method. * @param noProcessThreads * The threads to be ignored. * @return Returns <code>true</code> if the thread should be processed by thread cleaning features or * <code>false</code> if the thread should not be processed either because it is a JVM system thread or the * thread is contained by the <tt>noProcessThreads</tt> list. */ public static boolean processThread(Thread thread, ThreadSnapshot threadSnapshot, Thread... noProcessThreads) { if (noProcessThreads == null) { noProcessThreads = new Thread[0]; } return processThread(thread, threadSnapshot, Arrays.asList(noProcessThreads)); }
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]); for (Thread thread : threadArray) { if (processThread(thread, threadSnapshot, noProcessThreads)) { wasDirty = wasDirty || !finishThread(dryRun, thread, maxTimeoutMillis);
/** * @param filterJVMThreads * If <code>true</code> the JVM system threads are not part of the created snapshot. If <code>false</code> * also JVM system threads will be recorded by the thread snapshot. * @param filterCondition * (Optional) An optional filter condition * @return Returns a new {@link ThreadSnapshot} containing the currently active threads. */ public static ThreadSnapshot snapshot(boolean filterJVMThreads, ThreadFilter filterCondition) { Set<Integer> hashCodes = new HashSet<>(); Set<Thread> allThreads = ThreadCleaning.getAllThreads(); for (Thread t : allThreads) { boolean add = true; if (filterJVMThreads) { add = ThreadCleaning.processThread(t, null); } if (add) { if (filterCondition != null) { add = filterCondition.accept(t); } if (add) { hashCodes.add(System.identityHashCode(t)); } } } return new ThreadSnapshot(hashCodes); }
/** * This method checks if a thread should be processed by thread cleaning features. * * @param thread * The thread to check * @param threadSnapshot * Threads recorded by a thread snapshot are ignored by this method. * @param noProcessThreads * The threads to be ignored. * @return Returns <code>true</code> if the thread should be processed by thread cleaning features or * <code>false</code> if the thread should not be processed either because it is a JVM system thread or the * thread is contained by the <tt>noProcessThreads</tt> list. */ public static boolean processThread(Thread thread, ThreadSnapshot threadSnapshot, List<Thread> noProcessThreads) { // schuettec - 25.04.2017 : Ignore Daemon Threads because they will terminate if no other thread lives if (thread.isDaemon()) { return false; } ThreadGroup threadGroup = thread.getThreadGroup(); // Filter the shutdown thread and other java system threads if (noProcessThreads.contains(thread) || thread.getName() .equals("DestroyJavaVM") || (threadGroup != null && STATIC_THREADGROUP_FILTER.contains(threadGroup.getName())) || isStaticFilteredThreadName(thread.getName())) { return false; } else if (threadSnapshot != null && threadSnapshot.contains(thread)) { return false; } else { return true; } }
/** * Tries to finish all running user threads. JVM threads are filtered and will be ignored. * * @param maxTimeoutMillis * maximal time to spend on joining threads (can apply two times!). * @param threadSnapshot * Threads recorded by a thread snapshot are ignored by this method. * @param filtered * The threads that should be ignored. * @return Returns <code>true</code> if on of the threads could not be finished using an interrupt. <code>false</code> * is returned if all threads terminated cleanly. * */ public static boolean finishRunningThreads(int maxTimeoutMillis, ThreadSnapshot threadSnapshot, Thread... filtered) { boolean wasDirty = finishRunningThreads(false, maxTimeoutMillis, threadSnapshot, filtered); return wasDirty; }
/** * @return Returns the thread objects from this snapshot if they are still known by the JVM. */ public Set<Thread> getThreads() { Set<Thread> allThreads = new HashSet<>(); for (Thread t : ThreadCleaning.getAllThreads()) { int identityHashCode = System.identityHashCode(t); if (systemHashCodes.contains(identityHashCode)) { allThreads.add(t); } } return allThreads; }
/** * Tries to finish all running user threads. JVM threads are filtered and will be ignored. Uses the default maximal * time to spend on joining threads {@link #THREAD_WAIT_TIMEOUT} (can apply two times!). * * @param threadSnapshot * Threads recorded by a thread snapshot are ignored by this method. * @param filtered * The threads that should be ignored. * @return Returns <code>true</code> if one of the threads could not be finished using an interrupt. * <code>false</code> * is returned if all threads terminated cleanly. * */ public static boolean finishRunningThreads(ThreadSnapshot threadSnapshot, Thread... filtered) { return finishRunningThreads(THREAD_WAIT_TIMEOUT, threadSnapshot, filtered); }
/** * Tries to finish all running user threads. JVM threads are filtered and will be ignored. Uses the default maximal * time to spend on joining threads {@link #THREAD_WAIT_TIMEOUT} (can apply two times!). * * @param dryRun * If <code>true</code> this method does not really interrupt threads. * @param threadSnapshot * Threads recorded by a thread snapshot are ignored by this method. * @param filtered * The threads that should be ignored. * @return Returns <code>true</code> if on of the threads could not be finished using an interrupt. <code>false</code> * is returned if all threads terminated cleanly. * */ public static boolean finishRunningThreads(boolean dryRun, ThreadSnapshot threadSnapshot, Thread... filtered) { return finishRunningThreads(dryRun, THREAD_WAIT_TIMEOUT, threadSnapshot, filtered); }
wasDirty = ThreadCleaning.finishRunningThreads(threadSnapshot, Thread.currentThread()); if (wasDirty) { log.info("Engine didn't stopped cleanly - forcing termination.");