<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 with fixed number of thread. From Java 5.0+ one can get such pool from Executors using following method –
    public static ExecutorService 
                   newFixedThreadPool(int nThreads)
        Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

    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 10 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 fixed 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 Reader implements Callable<Properties> {

        
    private String name = null;

        
    public Reader(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 FixedThreadPoolExample {
        
    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 - just 20% of number of files.
                     
    */
                    executorPool 
    = Executors.newFixedThreadPool(files.length / 5);

                    
    for (String file : files) {
                        Callable
    <Properties> reader = new Reader(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:43 ゞ沉默是金ゞ 閱讀(909) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 国产成人1024精品免费| 国产国产人免费人成免费视频| 亚洲大码熟女在线观看| 亚洲欧洲日产国码久在线观看| 免费人成网站在线播放| 亚洲欧洲免费无码| 香港a毛片免费观看| 久久精品免费网站网| 国产午夜亚洲精品不卡| 亚洲欧美日韩自偷自拍| 亚洲成a人片毛片在线| 亚洲av日韩av天堂影片精品| 亚洲欧洲日产国码一级毛片| 在线日韩av永久免费观看| 色妞WWW精品免费视频| 精品成在人线AV无码免费看| 91av免费在线视频| 美女又黄又免费的视频| 亚洲成a人无码亚洲成www牛牛 | 免费播放美女一级毛片| 久久亚洲精品国产精品婷婷| 亚洲综合偷自成人网第页色| 亚洲黄色网址大全| 亚洲一区二区三区四区在线观看| 亚洲日韩国产精品第一页一区| 亚洲中文字幕视频国产| 亚洲国产精品人人做人人爱| 亚洲av无码国产精品色在线看不卡| 精品久久久久久久免费人妻| 全免费a级毛片免费看不卡| 成人最新午夜免费视频| 四虎影视免费在线| 永久黄网站色视频免费| 一个人看www在线高清免费看| 在线观看免费人成视频| 国产三级在线观看免费| 成人性生交视频免费观看| 最好免费观看韩国+日本| 青青草国产免费久久久91| 国产一级淫片a视频免费观看| 免费看国产精品麻豆|