Part 10: java.util.concurrent - ScheduledThreadPool Example
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 received and notify the cheque Clearing process.
We will try to make “Local Service”, which check the file reception 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 {
&nbs
补充:软件开发 , Java ,