Skip to content

Instantly share code, notes, and snippets.

@prashant4224
Created May 28, 2020 12:52
Show Gist options
  • Save prashant4224/c36ebd136ad53a92fce51da143b56e57 to your computer and use it in GitHub Desktop.
Save prashant4224/c36ebd136ad53a92fce51da143b56e57 to your computer and use it in GitHub Desktop.
/*--------- Executor Framework -----------*/
java.util.concurrent.Executors
Executor API de-couples execution of tasks from actual task to be executed via Executors
Executor framework to schedule and execute the submitted tasks and return the result from thread pool.
1. Overhead of thread creation & tear-down
2. For each process creating new thread occupy more memory & waste of resources.
ExecutorService executorService = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newFixedThreadPool();
ExecutorService executorService = Executors.newCachedThreadPool();
ExecutorService executorService = Executors.newScheduledThreadPool();
scheduledExecService.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
scheduledExecService.scheduleWithFixedDelay(Runnable command, long initialDelay, long period, TimeUnit unit)
Callable vs Runnable
A Callable -> implements call() method while a Runnable -> implements run() method.
A Callable can return a value but a Runnable cannot.
A Callable can throw checked exception but a Runnable cannot.
A Callable can be used with ExecutorService#invokeXXX(Collection<? extends Callable<T>> tasks) methods but a Runnable cannot be.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment