List of utility methods to do Thread Executor Execute
Future> | execute(String name, Runnable runnable) execute final Thread thread = new Thread(runnable, name); Future<?> future = new Future<Void>() { public boolean cancel(boolean mayInterruptIfRunning) { return false; public boolean isCancelled() { return false; public boolean isDone() { return !thread.isAlive(); public Void get() throws InterruptedException, ExecutionException { thread.join(); return null; public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { thread.join(unit.toMillis(timeout)); if (thread.isAlive()) { throw new TimeoutException("Thread is still alive after timeout"); return null; }; thread.start(); return future; |
void | executeAndWait(final Runnable r) execute And Wait final CountDownLatch countDown = new CountDownLatch(1); Runnable task = new Runnable() { public void run() { try { r.run(); } finally { System.err.println("scheduled"); countDown.countDown(); ... |
void | executeAsynchronously(Runnable r) execute Asynchronously pool.submit(r); |
Future> | executeForever(final Runnable runnable) execute Forever final AtomicBoolean keepRunning = new AtomicBoolean(true); final Future<?> future = Executors.newFixedThreadPool(1).submit(new Runnable() { public void run() { while (keepRunning.get()) { runnable.run(); }); ... |
Future | executeFuture(Callable execute Future return executorThreadPool.submit(callable);
|
Future> | executeInCachedPool(Runnable runnable) execute In Cached Pool Future<?> future = CACHED_THREAD_POOL.submit(runnable);
return future;
|
Future> | executeInThread(Runnable r) Posts a Runnable in a new Thread return exService.submit(r);
|
void | executeInThreadPool(Runnable runnable) execute In Thread Pool ExecutorService pool = Executors.newCachedThreadPool(); pool.execute(runnable); |
List | executeParallel(final List execute Parallel final int threadCount = callables.size() > 0 && callables.size() < maxThreadCount ? callables.size() : maxThreadCount; ExecutorService executor = newFixedThreadPool(threadCount); List<T> results = new ArrayList<>(); try { for (Future<T> future : executor.invokeAll(callables)) { results.add(future.get()); } finally { executor.shutdown(); return results; |
ScheduledFuture> | executePeriodicallyInThread(Runnable r, int delay, int period, TimeUnit unit) Posts a Runnable to be executed periodically in its own Thread . return timerExService.scheduleAtFixedRate(r, delay, period, unit);
|