<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竄行話避免讀寫錯亂,但是每個任務的提交不受其他任務的影響,不過樓主用英語寫博客不錯啊,好想把英語學好!  回復  更多評論
      
    主站蜘蛛池模板: 国产亚洲综合视频| 亚洲av永久中文无码精品 | 亚洲一区在线视频观看| 国产好大好硬好爽免费不卡| 亚洲精品无码久久一线| a国产成人免费视频| 国产亚洲精品精华液| 亚欧免费一级毛片| 97久久精品亚洲中文字幕无码| 91福利免费体验区观看区| 亚洲av永久无码精品三区在线4| 免费精品国产自产拍在线观看图片| 亚洲av无码不卡久久| 日韩免费三级电影| 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 中文字幕乱码一区二区免费| 久久精品国产亚洲网站| 日韩av无码久久精品免费| 亚洲国产午夜电影在线入口| 国产在线观看免费观看不卡| 亚洲av永久无码精品秋霞电影秋 | 成人无码精品1区2区3区免费看| 亚洲精品少妇30p| 四虎免费影院ww4164h| 亚洲一区二区三区高清在线观看 | 久久亚洲国产成人亚| 最近中文字幕无吗免费高清| 色偷偷亚洲第一综合| 亚洲精品乱码久久久久66| 亚洲视频免费一区| 色www免费视频| 亚洲AV无码一区二区三区DV| AA免费观看的1000部电影| 国产亚洲视频在线观看| 亚洲A∨无码一区二区三区| 成年人在线免费观看| 一日本道a高清免费播放| 91精品国产亚洲爽啪在线观看| 午夜两性色视频免费网站| 99久久精品毛片免费播放| 亚洲人成电影网站久久|