<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 reuse previously constructed threads when they are available. From Java 5.0+ one can get such pool from Executors using following method –
    public static ExecutorService newCachedThreadPool()
    Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.


    Example-
    Suppose we have 100 properties files in an application. We have one thread that can read properties file and return a map value. We want to optimize the time to read all 100 properties file by using concurrent reading. Here optimize means – we need a perfect balance between CPU Utilization and total time consumed by reading process.

    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 Cached 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 CachedReader implements Callable<Properties> {

        
    private String name = null;

        
    public CachedReader(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 CachedThreadPoolExample {
        
    public static String directory = "config";
        
    private static ExecutorService executorPool = null;
        
    private static int MAX_THREADS = 20;

        
    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 - Reuse the the threads.
                     
    */
                    executorPool 
    = Executors.newCachedThreadPool();

                    
    for (String file : files) {
                        Callable
    <Properties> reader = new CachedReader(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:47 ゞ沉默是金ゞ 閱讀(1570) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 久草免费福利在线| 免费无码一区二区三区蜜桃| 在线观看日本亚洲一区| 亚洲av永久中文无码精品综合| 一级毛片**免费看试看20分钟| 日本卡1卡2卡三卡免费| 成人黄软件网18免费下载成人黄18免费视频| 高清国语自产拍免费视频国产| 亚洲精品国产日韩无码AV永久免费网| 久久亚洲综合色一区二区三区| 亚洲国产成人精品激情| 一个人看的免费观看日本视频www| 亚洲精品国产综合久久一线| 中国videos性高清免费| 国产一区二区三区在线观看免费| 久久久亚洲欧洲日产国码是AV | 亚洲国产精品lv| 黄人成a动漫片免费网站| 人与禽交免费网站视频| 亚洲精品麻豆av| 午夜精品一区二区三区免费视频| 亚洲精品WWW久久久久久| 免费无码又爽又刺激网站直播| 久久精品国产亚洲香蕉| 黄视频在线观看免费| 亚洲国产精品第一区二区| 西西大胆无码视频免费| 亚洲国产精品自在线一区二区 | 四虎影视久久久免费| 在线观看亚洲免费视频| 亚洲综合一区无码精品| 免费一级e一片在线播放| 麻豆亚洲AV成人无码久久精品 | 久久永久免费人妻精品| ASS亚洲熟妇毛茸茸PICS| 亚洲国产成人久久综合碰| 黄色毛片免费观看| 亚洲AV日韩精品久久久久久| 久久永久免费人妻精品| 亚洲人成网站免费播放| 国产真实伦在线视频免费观看|