Was it helpful?

Question

Difference between Executor and ExecutorServices in Java

JavaServer Side ProgrammingProgramming

Executor and ExecutorServices both interfaces are part of the Executor framework. It is released with Java 5.

 In java, thread creation is very expensive operation so we should  reuse the available thread instead of starting a new thread every time and we can achieve the same using Executor framework.

Executor framework use the thread pool to execute the task parallel which helps to optimize response time and resource utilization. It provides four types of built in thread pools −

  • Fixed Thread Pool
  • Cached Thread Pool
  • Scheduled Thread Pool
  • Single Thread Executor
Sr. No.KeyExecutorExecutorServices
1
Basic
It is a parent interface
It extends Executor Interface
2
Method
It has execute() method
It has submit() method
3
Return Type
It does not return anything .
It return future object.
4.
Runnable /Callable
It accept runnable object.
It accept both runnable and callable

Example of ExecutorService

public class Main {
   public static void main(String args[]) {
      ExecutorService services = Executors.newSingleThreadExecutor();
      Future<?> future = services.submit(new Task());
   }
}
public class Task implements Runnable {
   @Override
   public void run() {
      System.out.println("In Run");
   }
}

Example of Executor

public class Main {
   public static void main(String args[]) {
      Executor executor = Executors.newSingleThreadExecutor();
      executor.execute(new Task());
   }
}
public class Task implements Runnable {
   @Override
   public void run() {
      System.out.println("In Run");
   }
}
raja
Published on 09-Sep-2020 13:04:56
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top