<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
    Submit Collection of Threads via ExecutorService

    Today in next part of the series we will talk about How we can release multiple threads via Executor Interface.

    As per JAVA 6.0 docs –
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)                                                            throws InterruptedException


    In traditional Java – If we have to release multiple threads- we have to create Thread objects and call
    Start method one by one.
    In Java 5.0 and above – If we can put all callable objects in a collection object and pass the collection objects to ExecutorService to release.

    For example – If we have 1000 thread object to be released, we can create an array list and an ExecutorService object with thread pool size of 20.

            // create an ExecutorService object with thread pool size = 20
            // create an array List with 1000 callable objects.
            // Release all threads by ExecutorService.

    Some important points to note here are

    • Ø As ExecutorService has thread pool of 20, it will internally manage queuing and releasing of 1000 threads.
    • Ø invokeAll is a blocking method. It means – JVM won’t proceed to next line until all the 1000 threads are complete.
    • Ø All the 1000 threads must have same Return Type < T >.
    • Ø Future.isDone() is true for each element of the returned list
    • Ø completed thread could have terminated either normally or by throwing an exception. In both the case return type of Future.isDone( ) is true.
    • Ø The results of this method are undefined if the given collection is modified while this operation is in progress
    • Ø It can throw following exceptions –
    • InterruptedException - if interrupted while waiting, in which case unfinished tasks are cancelled.
    • NullPointerException - if tasks or any of its elements are null.
    • RejectedExecutionException - if any task cannot be scheduled for execution.



    EXAMPLE –
    Imagine a scenario, one needs to write create a txn file of each request coming to system. Following are the requirements-
    • Ø File name will be the client name.
    • Ø At a time, almost 1000 clients could be connected at a time, so system must be capable of creating 1000 files in one go.
    • Ø Process of creating one file should be less than 5 ms.
    • Ø Total time of creating all the files should be less than 300 ms.


    Let’s write the pseudo code for this case –

    File creation Thread – implements CALLABLE<Boolean>
    // GET the File name and Raw Data in Constructor
    Try{
    // Create a File Object
    // Create a print Writer Object
    // Prepare the data and Write in the File
    // Close the print Writer and File Object
    // Return TRUE
    } catch Exception {
    return FALSE
    }
                                

    Main Parent Thread
      // -- In the Loop for whole Client list ---
        try{
            // create a file creator thread – Pass filename and raw Data
    // add the thread object into an Array List
    // Pass the array list to Executor Service interface and invokeAll.
    // Loop in the Future<Boolean> and check how many threads completed successfully.
      }catch Exception{
    // Take necessary action
    }finally{
    // Shut down the ExecutorService
     }

    package com.jovialjava.blog.threads;

    import java.io.File;
    import java.io.PrintWriter;
    import java.util.*;
    import java.util.concurrent.*;

    // FILE CREATOR THREAD
    class FileTask implements Callable<Boolean> {

        
    private String name = null;
        
    private String data = null;

        
    public FileTask(String name, String data) {
            
    this.name = name;
            
    this.data = data;
        }

        
    public Boolean call() {
            
    try {
                File file 
    = new File(this.name);
                PrintWriter writer 
    = new PrintWriter(file);
                writer.write(
    this.data);
                writer.close();
                
    return true;
            } 
    catch (Exception e) {
                
    return false;
            }
        }
    }

    // MAIN THREAD
    public class InvokeAllExample {

        
    private static final ExecutorService executorPool = Executors.newFixedThreadPool(20);
        
    private static final int NO_OF_CLIENT = 1000;
        
    private static final String FILE_EXT = ".txt";
        
    private static String TXN_DATA = "SOME RANDOM TXN DATA FOR CLIENT --> ";
        
    private static String DIRECTORY = "EXAMPLE" + File.separator;

        
    static {
            
    if (!new File(DIRECTORY).isDirectory()) {
                
    new File(DIRECTORY).mkdir();
            }
        }

        
    public static void main(String[] args) {
            
    int success = 0;
            
    int failure = 0;
            
    /**
             * Lets assume we have 1000 clients connected and sending request at a
             * time.
             
    */
            Collection
    <FileTask> collection = new ArrayList<FileTask>();
            
    for (int i = 0; i < NO_OF_CLIENT; i++) {
                FileTask task 
    = new FileTask(DIRECTORY + Integer.toString(i) + FILE_EXT, TXN_DATA + i);
                collection.add(task);
            }
            
    long startTime = new Date().getTime();
            
    try {
                List
    <Future<Boolean>> list = executorPool.invokeAll(collection);
                
    for (Future<Boolean> fut : list) {
                    
    int ignore = fut.get() ? success++ : failure++;
                }

            } 
    catch (Exception e) {
                e.printStackTrace();
            } 
    finally {
                System.out.println(
    "TOTAL SUCCESS - " + success);
                System.out.println(
    "TOTAL FAILURE - " + failure);
                System.out.println(
    "Total time - " + (new Date().getTime() - startTime) + " ms");
                executorPool.shutdown();
            }
        } 
    // End of Main

    }
    posted on 2012-08-06 10:40 ゞ沉默是金ゞ 閱讀(903) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 亚洲中文字幕久久无码| 亚洲∧v久久久无码精品| 亚洲日韩国产欧美一区二区三区| 久久久久久国产精品免费免费男同| 色久悠悠婷婷综合在线亚洲| 黄视频在线观看免费| 亚洲中文字幕无码久久综合网| eeuss影院免费直达入口| 中文字幕无码精品亚洲资源网| 人成免费在线视频| 国产中文在线亚洲精品官网| 国产无遮挡色视频免费观看性色| 国产AV无码专区亚洲AV手机麻豆| 中文字幕免费在线看| 亚洲AV无码久久精品成人| 99久久久国产精品免费蜜臀| youjizz亚洲| 国产伦精品一区二区三区免费迷 | 亚洲男人的天堂在线播放| 亚洲精品免费视频| 亚洲毛片一级带毛片基地| 最近最新中文字幕完整版免费高清 | 亚洲日韩国产精品乱-久| 免费观看理论片毛片| 日韩成人毛片高清视频免费看| 亚洲熟妇中文字幕五十中出| 麻豆成人久久精品二区三区免费| 亚洲六月丁香六月婷婷蜜芽| 免费看大美女大黄大色| www一区二区www免费| 久久精品国产亚洲AV无码麻豆| 色窝窝免费一区二区三区| 国产精品亚洲а∨无码播放不卡 | 久久精品亚洲视频| 性一交一乱一视频免费看| 国产大片免费天天看| 亚洲欧洲日产国码二区首页| 免费无码一区二区三区蜜桃大| 9i9精品国产免费久久| 亚洲另类图片另类电影| 亚洲精品天堂成人片?V在线播放|