Core Java Interview Questions: Executor Framework

January 03, 2014

Qns-1: What do you understand by Executor Framework in Java?

Ans: Executor Framework in java has been introduced in JDK 5. Executor Framework handles creation of thread, creating the thread pool and checking health while running and also terminates if needed.

Qns-2: What is the role of ExecutorService in Java?

Ans: ExecutorService provides different methods to start and terminate thread. There are two methods execute() and submit() in ExecutorService. Execute() method is used for threads which is Runnable and submit() method is used for Callable threads.

Qns-3: What is Executors in java Executor Framework?

Ans: Executors is a factory that provides the methods to return ExecutorService, ScheduledExecutorService, ThreadFactory. Find some method details.
newFixedThreadPool(): It returns the pool with fixed number of size. We need to pass the number of threads to this method. If concurrently task are submitted more than the pool size, then rest of task need to wait in queue. It returns ExecutorService.
newScheduledThreadPool: This also creates a fixed size pool but it can schedule the thread to run after some defined delay. It is useful to schedule the task. It returns ScheduledExecutorService.
newCachedThreadPool(): There is no fixed size of this pool. Thread will be created at run time and if there is no task it will alive for 60 second and then die. For short lived threads this pool works good. It returns ExecutorService.

Qns-4: 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.

Qns-5: 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.

Qns-6: 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)

Qns-7: What is the role of Executors.privilegedThreadFactory() in Executor Framework?

Ans: privilegedThreadFactory returns a thread factory that creates thread with same permission as main thread.

Qns-8: What is the role of Executors.unconfigurableExecutorService in Executor Framework?

Ans: unconfigurableExecutorService returns an object that delegates all methods of ExecutorService to the given executor so that any other method cannot accessed by cast.

Qns-9: 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.

Qns-10: How to get return value of a callable thread in java Executor Framework?

Ans: Using Future, we can get the return value of callable thread.
ExecutorService exService = Executors.newCachedThreadPool();
Future<Integer> future=exService.submit(new CallableThread());
int val=future.get();

©2023 | Privacy Policy | Contact Us