Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 scheduledThread pool and Single Thread Executor.
| Sr. No. | Key | Scheduled Thread Pool | Single Thread Executor |
|---|---|---|---|
| 1 |
Basic |
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically. |
Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time |
| 2 |
Queue |
It uses Delay Queue to store tasks. Schedule the task based on time delay. |
It uses blocking queue. |
| 3 |
Thread Lifetime |
T he number of threads to keep in the pool, even if they are idle |
Recreate thread if killed because of the task. |
| 4. |
Thread Pool Size |
It always has a single thread running. |
The thread pool can grow from zero threads to Integer.MAX_VALUE |
| 5. |
Use Case |
We should used fixedthreadpool, when we wanted to limit the concurrent task |
This type of pool can be used when you want to ensure that Task1 should always run before task2. |
Example of ScheduledThreadPool
public class ScheduledThreadPoolExample {
public static void main(String args[]) {
ScheduledExecutorService services = Executors.newScheduledThreadPool(10);
services.schedule(new Task(), 10, TimeUnit.SECONDS);
services.scheduleAtFixedRate(new Task(), 15, 10, TimeUnit.SECONDS);
//(new Thread(new Main())).start();
}
}
public class Task implements Runnable {
@Override
public void run() {
System.out.println("In Run");
}
}
Example of SingleThreadExecutor
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");
}
}Advertisements