ActivityQueue implements Closeable { public void start() { for ( QueueGroups group : QueueGroups.values() ) { final DelayQueue<ActivityHolder> thisQueue = queues.get(group); service.submit ( new Runnable() { @Override public void run() { try { while ( !Thread.currentThread().isInterrupted() ) { ActivityHolder holder = thisQueue.take(); try { Boolean result = holder.activity.call(); holder.activity.completed((result != null) && result); } catch ( Throwable e ) { log.error("Unhandled exception in background task", e); } } } catch ( InterruptedException dummy ) { Thread.currentThread().interrupt(); } } } ); } } ActivityQueue(); void start(); synchronized void add(QueueGroups group, Activity activity); synchronized void add(QueueGroups group, Activity activity, long delay, TimeUnit unit); @Override void close(); synchronized void replace(QueueGroups group, Activity activity); synchronized void replace(QueueGroups group, Activity activity, long delay, TimeUnit unit);  }
@Test public void test() throws Exception { final CountDownLatch latch = new CountDownLatch(2); final ReentrantLock lock = new ReentrantLock(); Thread t1 = new Thread ( new Runnable() { @Override public void run() { lock.lock(); latch.countDown(); } } ); Thread t2 = new Thread ( new Runnable() { @Override public void run() { lock.lock(); latch.countDown(); } } ); t1.start(); t2.start(); if ( latch.await(10, TimeUnit.SECONDS) ) { System.out.println("yep"); } else { System.out.println("nope"); } }