Javatpoint Logo
Javatpoint Logo

Java Executor class

Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes defined in this package. This class supports the following kinds of methods:

  • Methods that create and return an ExecutorService set up with commonly useful configuration settings.
  • Methods that create and return a ScheduledExecutorService set up with commonly useful configuration settings.
  • Methods that create and return a "wrapped" ExecutorService, that disables reconfiguration by making implementation-specific methods inaccessible.
  • Methods that create and return a ThreadFactory that sets newly created threads to a known state.
  • Methods that create and return a Callable out of other closure-like forms, so they can be used in execution methods requiring Callable.

Java Executor class declaration

List of Executors class Methods

NO Method Description
1. public static Callable<Object> callable(Runnable task)
public static <T> Callable<T> callable(Runnable task, T result)
public static Callable<Object> callable(PrivilegedAction<?> action)
public static Callable<Object> callable(PrivilegedExceptionAction<?> action)
The Callable() method of Executors class returns a Callable object that, when called, runs the given task and returns null.
2. public static ThreadFactory defaultThreadFactory() The defaultThreadFactory() method of Executors class Returns a default thread factory used to create new threads. This factory creates all new threads used by an Executor in the same ThreadGroup. If there is a SecurityManager, it uses the group of System.
3. public static ExecutorService newCachedThreadPool()
public static ExecutorService newCachedThreadPool (ThreadFactory threadFactory)
The newCachedThreadPool() method of Executors class Creates a thread pool that creates new threads as needed but will reuse previously constructed threads when they are available.
4. public static ExecutorService newFixedThreadPool(int nThreads)
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
The newFixedThreadPool() method of Executors class creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most n Threads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.
5. Public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)
The newScheduledThreadPool() method of Executors class Creates a thread pool that can schedule commands to run after a given delay or to execute periodically.
6. public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
The newSingleThreadExecutor() method of Executors class creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to the shutdown, a new one will take its place if needed to execute subsequent tasks.).
7. public static ExecutorService newWorkStealingPool()
public static ExecutorService newWorkStealingPool(int parallelism)
The newWorkStealingPool() method of Executors class creates a work-stealing thread pool using the number of available processors as its target parallelism level.
8. public static ThreadFactory privilegedThreadFactory() The privilegedThreadFactory() method of Executors class returns a thread factory used to create new threads that have the same permissions as the current thread. This factory creates threads with the same settings as defaultThreadFactory(), additionally setting the AccessControlContext and contextClassLoader of new threads to be the same as the thread invoking this privilegedThreadFactory method.

Java Executor class Example: privilegedThreadFactory()

Test it Now


Running thread concurrently

Java Executor Class Example: newCachedThreadPool()

Test it Now


size of mypool: 0
Total number threads scheduled): 2
Thread Name: pool-1-thread-2
Thread Name: pool-1-thread-1
after sleep Thread Name: pool-1-thread-2
after sleep Thread Name: pool-1-thread-1
Next TopicJava Executors

Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA