@Test(timeout = 2000) public void testDescendantThreadGroups() throws InterruptedException { final SecurityManager current = System.getSecurityManager(); try { System.setSecurityManager(new SecurityManager() { @Override public void checkAccess(ThreadGroup g) { final ThreadGroup source = Thread.currentThread().getThreadGroup(); if (source != null) { if (!source.parentOf(g)) { throw new SecurityException("source group is not an ancestor of the target group"); } super.checkAccess(g); } } @Override public void checkPermission(Permission perm) { } }); final AtomicReference<DefaultThreadFactory> factory = new AtomicReference<DefaultThreadFactory>(); final AtomicInteger counter = new AtomicInteger(); final Runnable task = new Runnable() { @Override public void run() { counter.incrementAndGet(); } }; final AtomicReference<Throwable> interrupted = new AtomicReference<Throwable>(); final Thread first = new Thread(new ThreadGroup("brother"), new Runnable() { @Override public void run() { factory.set(new DefaultThreadFactory("test", false, Thread.NORM_PRIORITY, null)); final Thread t = factory.get().newThread(task); t.start(); try { t.join(); } catch (InterruptedException e) { interrupted.set(e); Thread.currentThread().interrupt(); } } }); first.start(); first.join(); assertNull(interrupted.get()); final Thread second = new Thread(new ThreadGroup("sister"), new Runnable() { @Override public void run() { final Thread t = factory.get().newThread(task); t.start(); try { t.join(); } catch (InterruptedException e) { interrupted.set(e); Thread.currentThread().interrupt(); } } }); second.start(); second.join(); assertNull(interrupted.get()); assertEquals(2, counter.get()); } finally { System.setSecurityManager(current); } }
@Override public Thread newThread(Runnable r) { Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet()); try { if (t.isDaemon() != daemon) { t.setDaemon(daemon); } if (t.getPriority() != priority) { t.setPriority(priority); } } catch (Exception ignored) { } return t; }
DefaultThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet()); try { if (t.isDaemon() != daemon) { t.setDaemon(daemon); } if (t.getPriority() != priority) { t.setPriority(priority); } } catch (Exception ignored) { } return t; } }
DefaultThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet()); try { if (t.isDaemon() != daemon) { t.setDaemon(daemon); } if (t.getPriority() != priority) { t.setPriority(priority); } } catch (Exception ignored) { } return t; } DefaultThreadFactory(Class<?> poolType);  DefaultThreadFactory(String poolName);  DefaultThreadFactory(Class<?> poolType, boolean daemon);  DefaultThreadFactory(String poolName, boolean daemon);  DefaultThreadFactory(Class<?> poolType, int priority);  DefaultThreadFactory(String poolName, int priority);  DefaultThreadFactory(Class<?> poolType, boolean daemon, int priority);  DefaultThreadFactory(String poolName, boolean daemon, int priority, ThreadGroup threadGroup);  DefaultThreadFactory(String poolName, boolean daemon, int priority); }
DefaultThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet()); try { if (t.isDaemon() != daemon) { t.setDaemon(daemon); } if (t.getPriority() != priority) { t.setPriority(priority); } } catch (Exception ignored) { } return t; } DefaultThreadFactory(Class<?> poolType);  DefaultThreadFactory(String poolName);  DefaultThreadFactory(Class<?> poolType, boolean daemon);  DefaultThreadFactory(String poolName, boolean daemon);  DefaultThreadFactory(Class<?> poolType, int priority);  DefaultThreadFactory(String poolName, int priority);  DefaultThreadFactory(Class<?> poolType, boolean daemon, int priority);  DefaultThreadFactory(String poolName, boolean daemon, int priority, ThreadGroup threadGroup);  DefaultThreadFactory(String poolName, boolean daemon, int priority); static String toPoolName(Class<?> poolType); @Override Thread newThread(Runnable r); }
DefaultThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { Thread t = newThread(FastThreadLocalRunnable.wrap(r), prefix + nextId.incrementAndGet()); try { if (t.isDaemon() != daemon) { t.setDaemon(daemon); } if (t.getPriority() != priority) { t.setPriority(priority); } } catch (Exception ignored) { } return t; } DefaultThreadFactory(Class<?> poolType);  DefaultThreadFactory(String poolName);  DefaultThreadFactory(Class<?> poolType, boolean daemon);  DefaultThreadFactory(String poolName, boolean daemon);  DefaultThreadFactory(Class<?> poolType, int priority);  DefaultThreadFactory(String poolName, int priority);  DefaultThreadFactory(Class<?> poolType, boolean daemon, int priority);  DefaultThreadFactory(String poolName, boolean daemon, int priority, ThreadGroup threadGroup);  DefaultThreadFactory(String poolName, boolean daemon, int priority); static String toPoolName(Class<?> poolType); @Override Thread newThread(Runnable r);  }