Monday, August 7, 2017

Java Executor framework

- The Executor framework is an abstraction layer over the actual implementation of java multithreading so that the programmer only concentrates on the business logic implementation.
- It is the first concurrent utility framework in java and used for standardizing invocation, scheduling, execution and control of asynchronous tasks in parallel threads.
- Executor implementation in java uses thread pools which consists of worker threads. The entire management of worker threads is handled by the framework. So the overhead in memory management is much reduced compared to earlier multithreading approaches.
- all parallel works are considered as tasks instead of simple threads.
- The Java Executor framework creates tasks by using instances of Runnable or Callable.
- In case of Runnable, the run () method does not return a value or throw any checked exception. But Callable is a more functional version in that area. It defines a call () method that allows the return of some computed value which can be used in future processing and it also throws an exception if necessary.


In the java.util.concurrent package there are three interfaces:

Executor — Used to submit a new task. execute()
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.

Executor Interface
- Using an Executor it is possible to remove the manual creation of threads to execute a command.

public class MyRunnable implements Runnable {

  public void run() {

    // code to be executed

  }

}



Thread t = new Thread(new MyRunnable());

t.start();


can be replaced with

Executor executor = ... // Executor creation

executor.execute(new MyRunnable());


ExecutorService Interface

ExecutorService adds a more useful and advanced version method to execute commands, submit().

Passing a Callable to the submit method is possible to get a Future object and use it to retrieve the result of the asynchronous computation.

Additionally it is possible to shutdown an ExecutorService rejecting submissions of new commands. Using the shutdown() method all submitted commands will be executed before stopping the ExecutorService, but no new command is accepted. A call to shutdownNow() prevents waiting tasks to be executed and try to stop all currently executing commands.
 
Runnable myCommand1 = ...

Callable<String> myCommand2 = ...

ExecutorService executorService = ... // Build an executorService

executorService.submit(myCommand1);

// submit Accepts also a Callable

Future<String> resultFromMyCommand2 = executorService.submit(myCommand2);  

// Will wait for myCommand1 and myCommand2 termination

executorService.shutdown(); 

Runnable myCommand3 = ...;

// Will throw a RejectedExecutionException because no new task can be submitted

executorService.submit(myCommand3); 


ScheduledExecutorService Interface

The ScheduledExecutorService is used to schedule command executions after a given delay or periodically, it must be used as a replacement for Timer and TimerTask.

It uses the method schedule to run the command after a given delay of time, scheduleAtFixedRate() and scheduleWithFixedDelay() are used to execute a task periodically.



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().
- 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 single thread to execute commands with the method newSingleThreadScheduledExecutor().
- a ScheduledExecutorService with a fixed length pool of threads to execute scheduled commands with the method newScheduledThreadPool().



public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("" + i);
            executor.execute(worker);
          }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("Finished all threads");
    } 


==> we are creating fixed size thread pool of 5 worker threads. Then we are submitting 10 jobs to this pool, since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state, as soon as one of the job is finished, another job from the wait queue will be picked up by worker thread and get's executed.

 

No comments:

Post a Comment

Web Development

Design Phase:- Below all these represent different stages of the UX/UI design flow:- Wireframes represent a very basic & visual repr...