![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Executor and ExecutorServices in Java
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. | Key | Executor | ExecutorServices |
---|---|---|---|
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"); } }
Advertisements