@Test public void testNotificationExecutor() throws Exception { ListeningExecutorService executor = SingletonHolder.getDefaultNotificationExecutor(); ThreadPoolExecutor tpExecutor = (ThreadPoolExecutor) setAccessible(executor.getClass().getDeclaredField("delegate")).get(executor); BlockingQueue<Runnable> queue = tpExecutor.getQueue(); for (int idx = 0; idx < 100; idx++) { final int idx2 = idx; logger.info("Adding {}\t{}\t{}", idx, queue.size(), tpExecutor.getActiveCount()); executor.execute( new Runnable() { @Override public void run() { logger.info("in {}", idx2); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("out {}", idx2); } }); } executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); }
public static <T> T getInstance(Class<T> clazz, Object... params) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { if (INSTANCES.containsKey(clazz)) { return (T) INSTANCES.get(clazz); } else { T instance = SingletonHolder.getInstance(clazz, params); INSTANCES.put(clazz.getName(), instance); return instance; } }