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 }
/** * Stops the timer, cancelling all tasks * * @throws InterruptedException if interrupted while waiting for thread to return */ public void stop() { stopRunner(); java.util.List<Runnable> remaining_tasks = pool.shutdownNow(); for (Runnable task : remaining_tasks) { if (task instanceof Future) { Future future = (Future) task; future.cancel(true); } } pool.getQueue().clear(); try { pool.awaitTermination(Global.THREADPOOL_SHUTDOWN_WAIT_TIME, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { } for (Entry entry : tasks.values()) entry.cancel(); tasks.clear(); }
protected void init() { if (threadDecorator != null) pool.setThreadDecorator(threadDecorator); // pool.allowCoreThreadTimeOut(true); startRunner(); }
public boolean isShutdown() { return pool.isShutdown(); }
public int getQueueSize() { return pool.getQueue().size(); }
public void setKeepAliveTime(long time) { pool.setKeepAliveTime(time, TimeUnit.MILLISECONDS); }
public int getCurrentThreads() { return pool.getPoolSize(); }
public void setMaxThreads(int size) { pool.setMaximumPoolSize(size); }
public long getKeepAliveTime() { return pool.getKeepAliveTime(TimeUnit.MILLISECONDS); }
public int getMaxThreads() { return pool.getMaximumPoolSize(); }
public void setMinThreads(int size) { pool.setCorePoolSize(size); }
public int getMinThreads() { return pool.getCorePoolSize(); }
public void setThreadFactory(ThreadFactory factory) { pool.setThreadFactory(factory); }
public void setThreadDecorator(ThreadDecorator threadDecorator) { this.threadDecorator = threadDecorator; pool.setThreadDecorator(threadDecorator); }