ThreadPoolExecutor and ScheduledThreadPoolExecutor

ThreadPoolExecutor

ThreadPoolExecutor implements ExecutorService .It executes each submitted tasks using one of its internally pooled threads.Thread pools provide improved performance when executing large number of asynchronous tasks.This class provides many adjustable parameters and extensibility hooks. But usually ExecutorFactory methods are used to create thread pool.

You can configure the no of threads in the ThreadPoolExecutor using the following variables:-

  • corePoolSize
  • maximumPoolSize

The ThreadPoolExecutor automatically adjust the pool size as per the bounds set by corePoolSize and maximumPoolSize.When a new task is submitted and fewer than core pool threads are running , a new thread is created to handle the request ,even if other threads are idle.If there are more than corePoolSize threads but less than maximumPoolSize threads running, a new thread is created only if the queue is full. Typically maximum and core pool size are set at the time of construction but they can also be set dynamically using the setter methods for maximum and core pool size. If the corePoolSize and maximumPoolSize is same a fixed size thread pool is created.

The ThreadPoolExceutor has several constructors available and you can specify corePoolSize ,maximumPoolSize, keepAliveTime, timeUnit, BlockingQueue and also ThreadFactory and RejectedExceutionHandle

Output

ScheduledThreadPoolExecutor

ScheduledThreadPoolExceutor can additionally schedule commands to run after a given delay or to execute periodically. This class is preferable when multiple threads are needed or when the additional capabilities of ThreadPoolExecutor is required.

Delayed tasks are executed no sooner than they are enabled, but without any guarantee about when , after they are enabled ,they will commence.

When a submitted task is cancelled before it is run ,execution is suppressed. By default such a cancelled task is not automatically removed from the work queue until the delay lapses.This enabled further inspection and monitoring but it may also lead to unbounded retention of cancelled tasks. To avoid this you can set setRemoveOnCancelPolicy(boolean) to true, which causes tasks to be immediately removed from the work queue at the time of cancellation. Successive execution of a tasks scheduled via scheduledAtFixedRate or scheduledWithFixedDelay do not overlap.While different executions may be performed by different threads ,the effect of prior execution happen before those of subsequent ones.

The schedulexxx() methods of ScheduledPoolExceutor accept instance of Runnable or Callable.You can get the result of computation of the Callable instance via get() method of ScheduledFuture.

Example

In the above example a scheduled thread pool is created with a thread pool of 5 threads . A runnable and callable task is created which is passed to the executor service .The runnable task is scheduled to run with a fixed delay of 1 second and the callable task is scheduled to execute after a delay of 3 seconds.

Output

That’s all about the ThreadPoolExecutor and ScheduledThreadPoolExecutor .I hope you enjoyed reading this article.Happy Coding .

Published by Anna

Just a simple girl who loves to code. .I am primarily a Java developer by profession and I am here to share my love for coding with you all I hope you enjoy reading my blog. Happy Coding Guys :-)

Leave a comment

Your email address will not be published. Required fields are marked *