<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 uses single thread to execute tasks. From Java 5.0+ one can get such pool from Executors using following method –
    public static ExecutorService newSingleThreadExecutor()
        Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.



    Example-
    Suppose we have 100 properties files in an application. We have one thread that can read properties file and return a map value.

    Pseudo code – READER THREAD

    Config Reader implements Callable<Map<String, String>
    try{
        // Get the file name in the constructor of thread
        // Check if File exists
        // Read the file and retrun the map object
    }catch(Exception e){
            //release all the resource
            //return null
    }

    Main THREAD-
            // Get a Single thread pool from Executors
     try{
        // Get the list of all properties file in the directory
        // Create a READER THREAD by passing the name of file
        // store the READER thread in the a list
        //release all the thread in one go and get the Map objects
    }catch(Exception e){
                    //release all the resources
                    // print the stack trace
    }finally{
            //shutdown the thread pool
    }

    package com.jovialjava.blog.threads;

    import java.io.File;
    import java.io.FileInputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Properties;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;

    class SingleReader implements Callable<Properties> {

        
    private String name = null;

        
    public SingleReader(String name) {
            
    this.name = name;
        }

        
    public Properties call() {
            
    try {
                File f 
    = new File(name);
                Properties prop 
    = new Properties();
                
    if (f.exists() && f.canRead() && f.isFile()) {
                    FileInputStream in 
    = new FileInputStream(f);
                    prop.load(in);
                    
    return prop;
                } 
    else {
                    System.err.println(
    "Please check about this file:[" + f.getAbsolutePath() + "]");
                    
    return null;
                }
            } 
    catch (Exception e) {
                e.printStackTrace();
                
    return null;
            }
        }
    }

    public class SingleThreadPoolExample {
        
    public static String directory = "config";
        
    private static ExecutorService executorPool = null;

        
    public static void main(String args) {
            
    try {
                File dir 
    = new File(directory);
                
    if (dir.isDirectory()) {
                    List
    <Callable<Properties>> fileList = new ArrayList<Callable<Properties>>();
                    String[] files 
    = dir.list();
                    
    /**
                     * Optimization - Single thread executor.
                     
    */
                    executorPool 
    = Executors.newSingleThreadExecutor();

                    
    for (String file : files) {
                        Callable
    <Properties> reader = new SingleReader(dir.getAbsolutePath() + File.separator + file);
                        fileList.add(reader);
                    }
                    List
    <Future<Properties>> results = executorPool.invokeAll(fileList);
                    
    /**
                     * Check how many success and how many failure
                     
    */
                    
    int success = 0, failure = 0;
                    
    for (Future<Properties> result : results) {
                        
    if (result.get() == null) {
                            failure
    ++;
                        } 
    else {
                            success
    ++;
                        }
                    }
                    System.out.println(
    "Total number of files [" + fileList.size() + "]");
                    System.out.println(
    "Success Count [" + success + "]");
                    System.out.println(
    "Failure Count [" + failure + "]");
                } 
    else {
                    
    throw new IllegalArgumentException("There is no such directory name -" + directory);
                }
            } 
    catch (Exception e) {
                e.printStackTrace();
            } 
    finally {
                
    if (executorPool != null) {
                    executorPool.shutdown();
                }
            }
        }

    }
    posted on 2012-08-06 10:49 ゞ沉默是金ゞ 閱讀(993) 評論(1)  編輯  收藏 所屬分類: Java SE

    FeedBack:
    # re: Part 12: java.util.concurrent : SingleThreadPool Example
    2012-08-06 16:25 | NotNolyJava
    覺得這個例子用singleThreadExecutor有點牽強,singleThreadExecutor應該是用來人為的將并行的任務竄行化:一個文件100個任務想讀寫,這個時候將這些任務用singleThreadExecutor竄行話避免讀寫錯亂,但是每個任務的提交不受其他任務的影響,不過樓主用英語寫博客不錯啊,好想把英語學好!  回復  更多評論
      
    主站蜘蛛池模板: 亚洲视频免费观看| 狠狠躁狠狠爱免费视频无码| 57pao国产成永久免费视频| 亚洲国产成人片在线观看无码| 久久精品无码专区免费| 亚洲日韩欧洲乱码AV夜夜摸| a免费毛片在线播放| 国产亚洲真人做受在线观看| a级毛片高清免费视频就| 无码专区—VA亚洲V天堂| 99re在线视频免费观看| 久久久国产精品亚洲一区| 1000部啪啪未满十八勿入免费| 亚洲精品美女久久久久9999| 青青久在线视频免费观看| 亚洲欧洲无码AV不卡在线| 免费一级毛片在级播放| 美女网站在线观看视频免费的| 久久精品国产亚洲网站| 2021免费日韩视频网| 亚洲精华国产精华精华液好用| 亚洲精品成人片在线观看| 野花香在线视频免费观看大全| 亚洲无砖砖区免费| 成人爱做日本视频免费| 国产真人无码作爱免费视频| 综合自拍亚洲综合图不卡区| 免费精品人在线二线三线区别| 黄色一级视频免费| 亚洲va在线va天堂va不卡下载| 美女网站免费福利视频| 一级黄色免费网站| 精品亚洲国产成AV人片传媒| 日韩中文无码有码免费视频 | 91短视频免费在线观看| 亚洲人精品亚洲人成在线| 亚洲男人在线无码视频| 精品福利一区二区三区免费视频| 亚洲AV永久无码精品一福利| 亚洲精品二区国产综合野狼| 女性无套免费网站在线看|