protected void _run() { ConcurrentNavigableMap<Long, Entry> head_map; // head_map = entries which are <= curr time (ready to be executed) if (!(head_map = tasks.headMap(System.currentTimeMillis(), true)).isEmpty()) { final List<Long> keys = new LinkedList<Long>(); for (Map.Entry<Long, Entry> entry : head_map.entrySet()) { final Long key = entry.getKey(); final Entry val = entry.getValue(); pool.execute( new Runnable() { public void run() { val.execute(); } }); keys.add(key); } tasks.keySet().removeAll(keys); } if (tasks.isEmpty()) { no_tasks.compareAndSet(false, true); waitFor(); // sleeps until time elapses, or a task with a lower execution time is added } else waitUntilNextExecution(); // waits until next execution, or a task with a lower execution time // is added }