JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Java ScheduledThreadPoolExecutor Example

There are multiple ways to schedule a task in java. We have already Java timer to schedule a task but the problem with timers task is that you can execute one task at a time.So if the current task takes longer subsequent job will be delayed.
In this scenario, you can use Java ScheduledThreadPoolExecutor.This class is a part of Executor framework and provides facility to schedule a task rather than executing it immediately.
There are three methods which you can use to schedule task using ScheduledThreadPoolExecutor.
  1. schedule
  2. scheduleAtFixedRate
  3. scheduleWithFixedDelay

Java ScheduledThreadPoolExecutor Example:

Step 1: Create a Runnable task named “RunnableTask.java”.
Step 2:
Let’s understand more about above main method.
ScheduledThreadPoolExecutor also have factory method named newScheduledThreadPool in Executors class similar to newFixedThreadPoolExecutor and newCachedThreadPoolExectuor.
You need to call schedule method to schedule as a task.
Schedule method takes three arguments:
public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit):
Runnable command: It represents the task which you want to schedule.
long delay: It represents inital delay after which you want to schedule the task.
TimeUnit unit: It is time measure for delay argument.
If you notice, schedule method returns object of ScheduledFuture<?> which you can use to get the status of submitted jobs.
We have also called scheduledThreadPool.shutdown(), shutdown() method will bydefault wait for submitted task to execute and then terminate. You can change this policy using setExecuteExistingDelayedTasksAfterShutdownPolicy().
When you run above program, you will get below output:
Current Time = Fri May 19 00:20:39 IST 2017
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:20:46 IST 2017
pool-1-thread-2 Start Time for Task 2 Fri May 19 00:20:48 IST 2017
pool-1-thread-3 Start Time for Task 3 Fri May 19 00:20:50 IST 2017
pool-1-thread-1 End Time for Task 1 Fri May 19 00:20:51 IST 2017
pool-1-thread-2 End Time for Task 2 Fri May 19 00:20:53 IST 2017
pool-1-thread-3 End Time for Task 3 Fri May 19 00:20:55 IST 2017
Completed all threads

ScheduledThreadPoolExecutor’s scheduleAtFixedRate example:

ScheduledThreadPoolExecutor’s scheduleAtFixedRate is used to schedule a task after fix delay and then execute that task periodically.
scheduleAtFixedRate method takes four arguments:
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit)
Runnable command: It represents the task which you want to schedule.
long initialDelay: It represents initial delay after which you want to schedule the task.
long period: It represents period by which you want to repeat the task.
TimeUnit unit: It is time measure for delay argument.
Let’s do some changes to call scheduleAtFixedRate in ScheduledThreadPoolMain.
When you run above program, you will get below output:
Current Time = Fri May 19 00:56:29 IST 2017
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:56:36 IST 2017
pool-1-thread-2 Start Time for Task 2 Fri May 19 00:56:38 IST 2017
pool-1-thread-3 Start Time for Task 3 Fri May 19 00:56:40 IST 2017
pool-1-thread-1 End Time for Task 1 Fri May 19 00:56:41 IST 2017
pool-1-thread-2 End Time for Task 2 Fri May 19 00:56:43 IST 2017
pool-1-thread-3 End Time for Task 3 Fri May 19 00:56:45 IST 2017
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:56:46 IST 2017
pool-1-thread-2 Start Time for Task 2 Fri May 19 00:56:48 IST 2017
pool-1-thread-3 Start Time for Task 3 Fri May 19 00:56:50 IST 2017
pool-1-thread-1 End Time for Task 1 Fri May 19 00:56:51 IST 2017
pool-1-thread-2 End Time for Task 2 Fri May 19 00:56:53 IST 2017
pool-1-thread-3 End Time for Task 3 Fri May 19 00:56:55 IST 2017
Completed all threads
If Observe the output carefully:
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:56:36 IST 2017
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:56:46 IST 2017
Once task 1 started, next task scheduled after period of 10 secs which have provided in scheduleAtFixedRate method.

ScheduledThreadPoolExecutor’s scheduleWithFixedDelay example:

ScheduledThreadPoolExecutor’s scheduleWithFixedDelay is used to schedule a task after initial delay and then execute tasks with fixed delay after completion of the previous task.
Let’s do some changes to call scheduleWithFixedDelay in ScheduledThreadPoolMain.
Current Time = Fri May 19 00:20:39 IST 2017
Current Time = Fri May 19 00:50:53 IST 2017
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:51:00 IST 2017
pool-1-thread-2 Start Time for Task 2 Fri May 19 00:51:02 IST 2017
pool-1-thread-3 Start Time for Task 3 Fri May 19 00:51:04 IST 2017
pool-1-thread-1 End Time for Task 1 Fri May 19 00:51:05 IST 2017
pool-1-thread-2 End Time for Task 2 Fri May 19 00:51:07 IST 2017
pool-1-thread-3 End Time for Task 3 Fri May 19 00:51:09 IST 2017
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:51:15 IST 2017
pool-1-thread-2 Start Time for Task 2 Fri May 19 00:51:17 IST 2017
pool-1-thread-3 Start Time for Task 3 Fri May 19 00:51:19 IST 2017
pool-1-thread-1 End Time for Task 1 Fri May 19 00:51:20 IST 2017
pool-1-thread-2 End Time for Task 2 Fri May 19 00:51:22 IST 2017
pool-1-thread-3 End Time for Task 3 Fri May 19 00:51:24 IST 2017
Completed all threads
If Observe the output carefully:
pool-1-thread-1 End Time for Task 1 Fri May 19 00:51:05 IST 2017
pool-1-thread-1 Start Time for Task 1 Fri May 19 00:51:15 IST 2017
Once task 1 ended, next task scheduled after delay of 10 secs which have provided in scheduleWithFixedDelay method.

.