Wednesday, May 11, 2022

ExecutorService and Thread Pools in java

Thread Pools :  Java Thread pool is a pool of threads (a group of fixed-size threads is created using LinkedBlockingQueue) means a collection of worker threads, it represents a group of worker threads it will reused to execute number of task. A thread pool helps to improve performance by reducing the number of threads needed and managing their lifecycle.That are waiting for the job and reused many times. A thread from the thread pool is pulled out and assigned a job by the service provider. Essentially, threads are kept in the thread pool until they're needed, after which they execute the task and return the pool to be reused later. 

Executor : Executor is an interface.

void shutdown() : Initiates an orderly shutdown, it will just tell the executor service that it can't accept new tasks but previously submitted tasks are executed.

List<Runnable> shutdownNow() : Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

execute() : The execute() method is defined in the Executor interface. it cannot return anything because's return type is void. it accept only Runnable task. it is used to submit a task to the Executor framework for asynchronous execution. 

submit() : The submit() method is defined in the ExecutorService interface. it can return the result of computation because it has a return type of Future object. it accept both Callable and Runnable. it is used to submit a task to the Executor framework for asynchronous execution. ExecutorService interface is sub-interface of Executor interface.

public interface Executor

ExecutorService : ExecutorService is sub-interface of Executor interface.

public interface ExecutorService extends Executor

ScheduledExecutorService  : ScheduledExecutorService is sub-interface of ExecutorService

public interface ScheduledExecutorService extends ExecutorService

Executors : Executors is a class.

Below are the methods and inner class defined in Executors class.

public static ExecutorService newFixedThreadPool(int nThreads) : This is used to Creates a thread pool of fixed size that will be reuse to execute task using shared unbounded queue.The threads will exist  in the pool until it is explicitly shutdown.They will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.

For example if we have 1000 task and we have create thtead pool of size 50 than 50 task has taken in queue. a new one will take once existing thread is completed or terminated due to a failure during execution prior to shutdown. newFixedThreadPool Example In Java

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)

public static ExecutorService newWorkStealingPool(int parallelism) : newWorkStealingPool was introduced  in Java 8, a new type of thread pool as newWorkStealingPool(). Creates a work-stealing thread pool using all available processors as its target parallelism level.

  • newWorkStealingPool is an abstraction of ForkJoinPool.commonPool. Only the difference is Executors.newWorkStealingPool creates a pool in asynchronous mode and ForkJoinPool.commonPool doesn't.
  • Bydefault it creates a work-stealing thread pool using all available processors as its target parallelism level. 
  • Like ForkJoinPools, newWorkStealingPools Creates a thread pool that maintains enough threads to support the given parallelism level, and may use multiple queues to reduce contention.
  • The actual number of threads may grow and shrink dynamically.
  • ForkJoinPool.commonPool uses a last-in, first-out (LIFO) queue configuration, whereas Executors.newWorkStealingPool uses first-in, first-out approach (FIFO).
  • A work-stealing pool makes no guarantees about the order in which submitted tasks are executed.
  • Parallel level is the processors currently available on the machine.
  • Runtime.getRuntime().availableProcessors() – This is the number of processors available to the JVM.
  • It is based on work-stealing principle (work-stealing algorithm) : Work stealing is a scheduling strategy where worker threads that have finished their own tasks can steal pending tasks from other threads. 
/**
     * Creates a thread pool that maintains enough threads to support
     * the given parallelism level, and may use multiple queues to
     * reduce contention. The parallelism level corresponds to the
     * maximum number of threads actively engaged in, or available to
     * engage in, task processing. The actual number of threads may
     * grow and shrink dynamically. A work-stealing pool makes no
     * guarantees about the order in which submitted tasks are
     * executed.
     *
     * @param parallelism the targeted parallelism level
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code parallelism <= 0}
     * @since 1.8
     */
    public static ExecutorService newWorkStealingPool(int parallelism) {
        return new ForkJoinPool
            (parallelism,
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

    /**
     * Creates a work-stealing thread pool using all
     * {@link Runtime#availableProcessors available processors}
     * as its target parallelism level.
     * @return the newly created thread pool
     * @see #newWorkStealingPool(int)
     * @since 1.8
     */
    public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

public static ExecutorService newWorkStealingPool(int parallelism)

public static ExecutorService newSingleThreadExecutor()

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)

public static ExecutorService newCachedThreadPool()

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)

public static ScheduledExecutorService newSingleThreadScheduledExecutor()

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

public static ExecutorService unconfigurableExecutorService(ExecutorService executor)

public static ScheduledExecutorService   unconfigurableScheduledExecutorService(ScheduledExecutorService executor)

public static ThreadFactory defaultThreadFactory()

public static ThreadFactory privilegedThreadFactory()

static class DelegatedExecutorService extends AbstractExecutorService : This is static inner class in Excetutors class. A wrapper class that exposes only the ExecutorService methods of an ExecutorService implementation.

static class FinalizableDelegatedExecutorService extends DelegatedExecutorService : This is static inner class in Excetutors class.

static class DelegatedScheduledExecutorService extends DelegatedExecutorService implements ScheduledExecutorService  : This is static inner class in Excetutors class. A wrapper class that exposes only the ScheduledExecutorService  methods of a ScheduledExecutorService implementation.




No comments:

Post a Comment