This article will discuss about Thread pool that can schedule threads to run after a specified interval of time. From Java 5.0 + one can get such pool from Executors using following method-
Public static ScheduledExecutorService
NewScheduledThreadPool (int corePoolSize)
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
The return type of this method (return type of thread pool) is ScheduledExecutorService. Some of the salient features of ScheduledExecutorService are-
Schedule a Callable or Runnable to run once with a fixed delay after submission
Schedule a Runnable to run periodically at a fixed rate
Schedule a Runnable to run periodically with a fixed delay between executions
Submission returns a ScheduledFutureTask handle which can be used to cancel the task
. Like Timer, but supports pooling
Example:
Suppose we are building a Bank check processing system. Here is the process-
Every local branch collect cheques and create a txt file contain cheque info.
A service runs which copy the cheque txt file from local branch to main server.
A Local service runs on server which check if any file has ED and has y the cheque Clearing process.
We will try to make "Local Service", which check the file placement and "Copying process" Which copy file from client machine to Server.
Pseudo Code-
// Define Local & Copying service running interval time in Seconds
// Make an ScheduledThreadPool with pool size 2
Try {
// Make Local Service thread
// Make Copying process thread
// Scheduled Both thread to run at regular interval
} Catch (Exception e ){
// Release all resources
}
LocalService Thread-
Try {
// Check if directory exists
// Check if any file exists in directory
// Return status
} Catch (Exception e ){
// Print necessary exception
}
Copying Process Thread-
Try {
// Check if File existin on remote server
// Copy the file to main server
} Catch (Exception e ){
// Print necessary exception
}
Package com. jovialjava. blog. threads;
Import java. io. File;
Import java. util. concurrent. Executors;
Import java. util. concurrent. ScheduledExecutorService;
Import java. util. concurrent. TimeUnit;
// LOCAL SERVICE THREAD
Class LocalService implements Runnable {
Private String DIRECTORY = null;
Public LocalService (String DIRECTORY ){
This. DIRECTORY = DIRECTORY;
}
Public void run (){
Try {
File dir = new File (this. DIRECTORY );
If (dir. isDirectory ()){
If (dir. list (). length> 0 ){
System. out. println ("file exists ");
}
} Else {
System. err. println ("no such directory [" + dir. getAbsolutePath () + "] exists ");
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
// COPYING SERVICE THREAD
Class CopyService implements Runnable {
Private String REMOTE_DIR = null;
Private String LOCAL_DIR = null;
Public CopyService (String remoteDir, String localDir ){
This. REMOTE_DIR = remoteDir;
This. LOCAL_DIR = localDir;
}
Public void run (){
Try {
File remote = new File (this. REMOTE_DIR );
File local = new File (this. LOCAL_DIR );
If (remote. isDirectory () & local. isDirectory ()){
If (remote. list (). length> 0 ){
System. out. println ("remote file found, COPYING ");
// --- Call the file copying method.
} Else {
System. out. println ("no remote file found ");
}
} Else {
System. err. println ("please check directory [" + remote. getAbsolutePath () + "OR/AND"
+ Local. getAbsolutePath () + "] existence ");
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Public class ScheduledExample {
Private static final ScheduledExecutorService executor = Executors. newScheduledThreadPool (2 );
Private static final int LOCAL_INTERVAL = 5, COPY_INTERVAL = 2;
Private static final String REMOTE_DIR = "REMOTE", LOCAL_DIR = "LOCAL ";
Public static void main (String args ){
Runnable localService = new LocalService (LOCAL_DIR );
Runnable remoteService = new CopyService (REMOTE_DIR, LOCAL_DIR );
Try {
Executor. scheduleWithFixedDelay (localService, 0, LOCAL_INTERVAL, TimeUnit. SECONDS );
Executor. scheduleWithFixedDelay (remoteService, 0, COPY_INTERVAL, TimeUnit. SECONDS );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Author: silence is gold