Executor Framework

Executor Interface

  • The most important root interface in executor interface.
  • It has only one method
    void execute(Runnable task);

ExecutorService Interface

  • Extends Executor interface.
  • Provides an lifecycle to an executor i.e an initialization phase, service phase and destruction phase.
  • Some of the methods provided by ExecutorService are
    <T> Future <T> submit(Callable<T> task)
    <T> Future <T> submit(Runnable<T> task)
    void shutdown()
    List<Runnable> shutdownNow()
    boolean isShutdown()

Executors Class

  • It is a factory class that constructs and returns various kinds of instances of ExecutorService with commonly used Executors setting.
  • public static ExecutorsService newFixedThreadPool(int nThreads);
    • Specifies the number of threads you want in the pool.
  • public static ExecutorsService newCachedThreadPool(int nThreads);
    • This kind of ExecutorsService keeps on creating thread until no thread is available
  • public static ExecutorsService newSingleThreadExecutor();
    • Background thread that executes one by one from a queue sequentially
    • Never executes more than one thread simultaneously
  • public static ExecutorsService newSingleThreadScheduledExecutor();
    • Executes only one thread at a time.
    • Can schedules the delay when the thread has to be executed.

Leave a Comment