<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ゞ沉默是金ゞ

    魚離不開水,但是沒有說不離開哪滴水.
    posts - 98,comments - 104,trackbacks - 0
    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 –
    1.         Schedule a Callable or Runnable to run once with a fixed delay after submission
    2.         Schedule a Runnable to run periodically at a fixed rate
    3.         Schedule a Runnable to run periodically with a fixed delay between executions
    4.         Submission returns a ScheduledFutureTask handle which can be used to cancel the task
    5.  .Like Timer, but supports pooling


    Example:
    1. Suppose we are building a Bank check processing system. Here is the process –
    2.  Every local branch collect cheques and create a txt file contain cheque info.
    3.  A service runs which copy the cheque txt file from local branch to main server.
    4.  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 {
                executor.scheduleWithFixedDelay(localService, 
    0, LOCAL_INTERVAL, TimeUnit.SECONDS);
                executor.scheduleWithFixedDelay(remoteService, 
    0, COPY_INTERVAL, TimeUnit.SECONDS);
            } 
    catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    posted on 2012-08-06 10:45 ゞ沉默是金ゞ 閱讀(862) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 亚洲AV成人精品网站在线播放| 每天更新的免费av片在线观看| 国产在线观看免费视频播放器| 亚洲手机中文字幕| 中文字幕在线观看免费视频| 久久综合亚洲鲁鲁五月天| 最近中文字幕无免费| 亚洲欧洲综合在线| 美女视频黄a视频全免费| 亚洲一级黄色大片| 黄网址在线永久免费观看 | 亚洲精品无码激情AV| 杨幂最新免费特级毛片| 国产午夜亚洲精品理论片不卡| 国产精品免费久久久久影院| 国产成A人亚洲精V品无码性色| 玖玖在线免费视频| 亚洲午夜久久久久久尤物| 大地资源在线观看免费高清| 婷婷亚洲综合五月天小说在线| 亚洲精品无码99在线观看| 日韩免费高清大片在线| 亚洲中文字幕久久精品无码2021| 免费看AV毛片一区二区三区| 一道本不卡免费视频| 久久精品国产亚洲av日韩| 国产成人A在线观看视频免费| 免费国产va视频永久在线观看| 国产亚洲?V无码?V男人的天堂 | 麻豆一区二区免费播放网站| 亚洲爆乳AAA无码专区| 亚洲色无码专区在线观看| 国产黄色免费网站| 国产亚洲美女精品久久| 亚洲av无码乱码国产精品fc2| 无码国产精品一区二区免费式影视 | 亚洲黑人嫩小videos| 午夜免费福利在线| a视频免费在线观看| 亚洲深深色噜噜狠狠网站| 久久久久亚洲AV无码专区桃色|