Tuesday, March 3, 2020

Interview in Sapient on Executor Framework - Part - II

Hi Friends,

In this post I'm extending previous post  Interview in Sapient on Executor framework interview questions asked in Sapient.

Hope these questions help everybody in their interview preparation.


You all can also read questions-answers from my other posts:





Question 1:

What is the difference between submit() and execute() methods in executor framework?


Answer:

There are multiple differences between submit() and execute() methods in executor framework which are described below:


  • execute() method is declared in Executor interface. While submit() method is declared in ExecutorService interface which extends Executor interface.
  • execute() method can take only Runnable object as parameter. While submit() has 3 overloaded forms which accept Runnable , Callable , Runnable and result type.
  • execute() method returns void. While all forms of submit() method returns Future instance.

Note: A thread can be created only with Runnable instance.



Question 2:

When result will be returned by Future.get() , when we call submit(Runnable instance)?

Answer:

As we know, run() method in Runnable doesn't return anything. So, Future instance in this case will not hold any result and calling get() on Future object will return null upon successful completion.



Question 3:

How to execute a collection of tasks at once in Executor Framework?

Answer:

We can call invokeAll() method from ExecutorService passing a collection of all tasks in it.

Syntax:

List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)



Question 4:

Describe the purpose and use case of Fork/Join framework.

Answer:

The fork/join framework allows parallelizing recursive algorithms. The main problem with parallelizing recursive algorithms with ThreadPoolExecutor is that we may quickly run out of threads because each recursive step requires its own thread.

The Fork/Join framework entry point is ForkJoinPool class which is an implementation of ExecutorService. It implements the work-stealing algorithm , where idle threads try to steal work from busy threads.
This allows to spread the calculations among  various threads and make progress while using fewer threads than it would require with a usual thread pool.


Question 5:

How many ways are there to create a thread and run it?

Answer:

There are 3 ways to create and run threads in java:

First way:

Thread t = new Thread(() -> System.out.println("Running thread using lambda expression"));
t.start();



Second way:

Thread t = new Thread(new Runnable(){

    public void run(){

        System.out.println("Running thread using Runnable instance");
    }

});

t.start();



Third way:

Thread t = new Thread(){

    public void run(){

        System.out.println("Running thread using anonymous class");
    }
}

t.start();



Question 6:

What are the available implementations of ExecutorService interface?

Answer:

There are mainly 3 implementations of ExecutorService interface.


  • ThreadPoolExecutor
  • ScheduledThreadPoolExecutor
  • ForkJoinPool



Question 7:

What special guarantee does the Java Memory Model hold for final fields of a class?

Answer:

Java Memory Model guarantees that final fields of the class will be initialized before any thread gets hold of the object.
Without this guarantee, an object of the class may be published i.e. become visible to other threads before all the fields of this object are initialized due to reorderings or other optimizations. This could cause racy access to these fields.



That's all for this post.

Thanks for reading!!


No comments:

Post a Comment

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...