/** * 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; } }
/** * @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; }
/** * @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); }