Javatpoint Logo
Javatpoint Logo

Java ThreadPoolExecutor class

Executors class provides simple implementation of ExecutorService using ThreadPoolExecutor, but ThreadPoolExecutor provides much more feature than that. We can specify the number of threads that will be alive when we create ThreadPoolExecutor instance, and we can limit the size of the thread pool and create our RejectedExecutionHandler implementation to handle the jobs that can't fit in the worker queue.

Signature

List of ThreadPoolExecutor class methods

Let's see the Java ThreadPoolExecutor methods are given below.

SN Method Description
1 protected void afterExecute(Runnable r, Throwable t) The afterExecute () method is invoked after execution of the given Runnable. This method is invoked by the same thread that executed the task.
2 protected void beforeExecute(Thread t, Runnable r) The beforeExecute() method invoked before executing the given Runnable in the given thread. This method is invoked by thread t that will execute task r, and may be used to re-initialize ThreadLocals, or to perform logging.
3 public void allowCoreThreadTimeOut(boolean value) The allowCoreThreadTimeOut() method set policy which says core thread will terminate if no tasks arrive within the keep-alive time, being replaced if needed when new tasks arrive.
4 public boolean allowsCoreThreadTimeOut() The allowsCoreThreadTimeOut() method returns true if this pool allows core threads to time out and terminate if no tasks arrive within the keepAlive time, being replaced if needed when new tasks arrive.
5 public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException The awaitTermination() method blocks until all the task in thread pool have completed execution or time for execution elapses.
6 public void execute(Runnable command) The execute() executes the given task sometime in the future. The task may execute in a new thread or an existing pooled thread. If the task cannot be submitted to the pool however because the pool is shutdown or any other reason, then the task is handled by the current RejectedExecutionHandler.
7 public int getActiveCount() The getActiveCount() method returns the approximate number of threads that are actively executing tasks.
8 public long getCompletedTaskCount() The getCompletedTaskCount() returns the approximate total number of tasks that have completed execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls.
9 public int getCorePoolSize() The getCorePoolSize() method returns the approximate number of threads that are actively executing tasks.
10 public long getKeepAliveTime(TimeUnit unit) The getKeepAliveTime () returns the thread keep-alive time, which is the amount of time that threads may remain idle before being terminated.
11 public int getLargestPoolSize() The getLargestPoolSize() method returns the largest number of threads that have ever simultaneously been in the pool.
12 public int getMaximumPoolSize() The getMaximumPoolSize () returns the maximum allowed number of threads.
13 public int getPoolSize() The getPoolSize() method returns the current number of threads in the pool.
14 public BlockingQueue getQueue() The getQueue() returns the task queue used by this executor. Access to the task queue is intended primarily for debugging and monitoring. This queue may be in active use. Retrieving the task queue does not prevent queued tasks from executing.
15 public RejectedExecutionHandler getRejectedExecutionHandler() The getRejectedExecutionHandler() method returns the current handler for unexecutable tasks.
16 public long getTaskCount() The getTaskCount() returns the approximate total number of tasks that have ever been scheduled for execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation.
17 public ThreadFactory getThreadFactory() The getThreadFactory() method returns the thread factory used to create new threads.
18 public boolean isShutdown() The isShutdown() returns true if this executor has been shut down.
19 public boolean isTerminated() The isTerminated() method returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.
20 public boolean isTerminating() The isTerminating() returns true if this executor is in the process of terminating after shutdown() or shutdownNow() but has not completely terminated.
21 public void setCorePoolSize(int corePoolSize) The setCorePoolSize() method Sets the core number of threads in the pool. Size of the pool is passed as a parameter.
22 public void setKeepAliveTime(long time, TimeUnit unit) The setKeepAliveTime() method sets the thread keep-alive time, which is the amount of time that threads may remain idle before being terminated.
23 public void setMaximumPoolSize(int maximumPoolSize) The setMaximumPoolSize () method sets the maximum allowed number of threads. This overrides any value set in the constructor.
24 public void shutdown() The shutdown() method Initiates an orderly shutdown in which already submitted task is accepted, but no new task is accepted.
25 public List shutdownNow() The shutdownNow () method Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
26 public String toString() The toString() method returns the String representation of the ThreadPoolExecutor object.
27 public int prestartAllCoreThreads() The preStartAllCoreThreads() method starts all core threads, causing them to wait for work idly. This overrides the default policy of starting core threads only when new tasks are executed.
28 public boolean prestartCoreThread() The preStartCoreThread() method starts a core thread, causing it to wait for work idly. This overrides the default policy of starting core threads only when new tasks are executed.
29 public void purge() The purge() method tries to remove from the work queue all Future tasks that have been canceled.
30 public boolean remove(Runnable task) The remove () method removes this task from the executor's internal queue if it is present, thus causing it not to be run if it has not already started.

Java ThreadPoolExecutor Class Example: getPoolSize()

Test it Now

Output:

POOL SIZE : 1
LARGEST POOL SIZE AFTER SUBMIT:2
Running Thread Name: pool-1-thread-1
Running Thread Name: pool-1-thread-2
Completed Thread Name: pool-1-thread-1
Completed Thread Name: pool-1-thread-2

Java ThreadPoolExecutor Class Example: getQueue ()

Test it Now

Output:

POOL SIZE : 1
get Queue:[]
Running Thread Name: pool-1-thread-2
Running Thread Name: pool-1-thread-1
Completed Thread Name: pool-1-thread-2
Completed Thread Name: pool-1-thread-1

Java ThreadPoolExecutor Class Example: setMaximumPoolSize ()

Test it Now

Output:

MAXIMUM POOL SIZE BEFORE SUBMIT: 10
 POOL SIZE AFTER SUBMIT:2
Running Thread Name: pool-1-thread-1
Running Thread Name: pool-1-thread-2
Completed Thread Name: pool-1-thread-2
Completed Thread Name: pool-1-thread-1






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA