Java Utililty Methods Thread Executor Execute

List of utility methods to do Thread Executor Execute

Description

The list of methods to do Thread Executor Execute are organized into topic(s).

Method

Futureexecute(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;
voidexecuteAndWait(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();
...
voidexecuteAsynchronously(Runnable r)
execute Asynchronously
pool.submit(r);
FutureexecuteForever(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();
});
...
FutureexecuteFuture(Callable callable)
execute Future
return executorThreadPool.submit(callable);
FutureexecuteInCachedPool(Runnable runnable)
execute In Cached Pool
Future<?> future = CACHED_THREAD_POOL.submit(runnable);
return future;
FutureexecuteInThread(Runnable r)
Posts a Runnable in a new Thread
return exService.submit(r);
voidexecuteInThreadPool(Runnable runnable)
execute In Thread Pool
ExecutorService pool = Executors.newCachedThreadPool();
pool.execute(runnable);
ListexecuteParallel(final List> callables, final int maxThreadCount)
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;
ScheduledFutureexecutePeriodicallyInThread(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);