ThreadUtil { static public Thread[] getAllThreadsMatching(final String regex) { if (regex == null) throw new NullPointerException("Null thread name regex"); final Thread[] threads = getAllThreads(); ArrayList<Thread> matchingThreads = new ArrayList<Thread>(); for (Thread thread : threads) { if (thread.getName().matches(regex)) { matchingThreads.add(thread); } } return matchingThreads.toArray(new Thread[0]); }  static ThreadGroup getRootThreadGroup(); static Thread[] getAllThreads(); static Thread getThread(final long id); static Thread[] getAllThreadsMatching(final String regex);  }
@Test public void getAllThreadsMatching() { Thread[] allThreads0 = ThreadUtil.getAllThreadsMatching("main"); Assert.assertEquals(1, allThreads0.length); Thread[] allThreads1 = ThreadUtil.getAllThreadsMatching("(?i).*Main.*"); Assert.assertEquals(1, allThreads1.length); Assert.assertEquals(allThreads0[0].getId(), allThreads1[0].getId()); Thread[] allThreads2 = ThreadUtil.getAllThreadsMatching(".*main.*"); Assert.assertEquals(1, allThreads2.length); Assert.assertEquals(allThreads0[0].getId(), allThreads2[0].getId()); Thread[] allThreads3 = ThreadUtil.getAllThreadsMatching(".*Main.*"); Assert.assertEquals(0, allThreads3.length); }