Skip to content

Instantly share code, notes, and snippets.

@vikasverma787
Created February 25, 2018 16:06
Show Gist options
  • Save vikasverma787/464017efb7a190fcf8782313ebaf29eb to your computer and use it in GitHub Desktop.
Save vikasverma787/464017efb7a190fcf8782313ebaf29eb to your computer and use it in GitHub Desktop.
--------------------------------------------------------------
In the java.util.concurrent package there are three interfaces:
Executor — Used to submit a new task.
ExecutorService — A subinterface of Executor that adds methods to manage lifecycle of threads used to run the submitted
tasks and methods to produce a Future to get a result from an asynchronous computation.
ScheduledExecutorService — A subinterface of ExecutorService, to execute commands periodically or after a given delay.
---------------------------------------------------------------
*** How to Create an Executor ***
To create an Executor it is possible to use the factory Executors class.
Most common methods are used to create:
* an ExecutorService with a single thread to execute commands with method newSingleThreadExecutor.
* a ScheduledExecutorService with a single thread to execute commands with the method newSingleThreadScheduledExecutor.
* an ExecutorService that use a fixed length pool of threads to execute commands with the method newFixedThreadPool.
* an ExecutorService with a pool of threads that creates a new thread if no thread is available and reuse an existing
thread if they are available with newCachedThreadPool.
* a ScheduledExecutorService with a fixed length pool of threads to execute scheduled commands with the method newScheduledThreadPool.
---------------------------------------------------------------
** ExecutorService Implementations **
Since ExecutorService is an interface, you need to its implementations in order to make any use of it. TheExecutorService has
the following implementation in the java.util.concurrent package:
• ThreadPoolExecutor
• ScheduledThreadPoolExecutor
Creating a ThreadPoolExecutor
The ThreadPoolExecutor has several constructors available. For instance:
int corePoolSize = 5;
int maxPoolSize = 10;
long keepAliveTime = 5000;
ExecutorService threadPoolExecutor =
new ThreadPoolExecutor(corePoolSize,maxPoolSize,keepAliveTime,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
----------------------------------------------------------------------
What is the role of FutureTask and Future in java?
Ans: FutureTask is a cancellable asynchronous computation in java. It can cancel the task which is running.
Once the FutureTask will be cancelled, it cannot be restarted.
Future is result of asynchronous computation. Future checks if task is complete and if completed it gets the output.
-----------------------------------------------------------------------
How to terminate a thread in Executor Framework in java?
Ans: ExecutorService provides a method awaitTermination(long timeout, TimeUnit unit) that takes time and unit of time as
an arguments. After that time thread pool is terminated. Suppose we need to terminate a task just now, then we can do as
ExecutorService.awaitTermination(0, TimeUnit.SECONDS)
-----------------------------------------------------------------------
What is difference between shutdownNow() and shutdown() in Executor Framework in java?
Ans: shutdown() and shutdownNow() methods belongs to ExecutorService. shutdown() method tries to stop the threads and do not
accept new task to execute but it completes the execution which has been submitted. shutdownNow() methods also tries to stop the
running threads and will not execute any task which has been submitted but not started.
---------------------------------------------------------------------------
What are the different policy in Executor Framework?
Ans: There are different policy within ThreadPoolExecutor in java.
a. ThreadPoolExecutor.AbortPolicy : AbortPolicy is a handler for rejected task. It handles those task which has been rejected.
b. ThreadPoolExecutor.CallerRunsPolicy : This also handles the rejected task and runs the rejected task directly.
c. ThreadPoolExecutor.DiscardOldestPolicy : This handles those rejected task that is oldest and unhandled. It discards those that oldest task.
d. ThreadPoolExecutor.DiscardPolicy : This is the handler for those rejected task that are rejected silently.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment