<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
    Future<T>:
    A Future represents the result of an asynchronous computation. Future has been provide with some really powerful methods. For Instance -
    1) We can check whether a task has been completed or not.
    2) We can cancel a task.
    3) Check whether task was cancelled or complete normally.


    Methods-


    boolean cancel(boolean mayInterruptIfRunning)
    This method does following -
    1) If the process(Thread) has not started, then cancel the thread.
    2) If the process has started, then check if 
    mayInterruptIfRunning = true ==> Inturrept the thread and cancel it
    mayInterruptIfRunning = false ==> Let it run
    This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.
    After this method returns, subsequent calls to isDone() will always return true.
    Subsequent calls to isCancelled() will always return true if this method returned true.


    boolean isCancelled()
        Returns true if this task was cancelled before it completed normally


    V get() throws InterruptedException, ExecutionException
    This method is a blocking call. It will cause JVM to wait if necessary for the computation to complete, and then retrieves its result.


    boolean isDone()
        Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation - in all of these cases, this method will return true.

    package com.jovialjava.blog.threads;

    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;

    public class FutureExample {
        
    /**
         * Executor pool is a new concept in JAVA 5+, it can help to release 
         * a thread. It maintains a pool of threads, It reuses them, also align them in 
         * a queue. Here we are creating pool of 1 thread.
         
    */
        
    private static final ExecutorService executorPool=Executors.newFixedThreadPool(5);
        
        
    public static void main(String args){
            CallableTask call 
    = new CallableTask();        
            
    try{
                
    /**
                 * Future<T> is result of asynchronous computation.
                 * It may possible that CallableTask has not started yet
                 * but 'ExecutorService' gives the result via Future
                 * Object.
                 
    */
                Future
    <String> future = executorPool.submit(call);
                
    /**
                 * We can check if CallableTask has been completed.
                 
    */
                System.out.println(
    "Status of Callable Task [Is Completed ? "+ future.isDone()+ "]");
                
    /**
                 * We can get the result of callable Task.
                 * Note : future.get() is a blocking call, It will wait until the associated process finishes.
                 
    */
                System.out.println(
    "Result of callable task ["+ future.get()+"]");
                
    /**
                 * We can cancel the task.
                 
    */
                System.out.println(
    "Trying to cancel the task [Is Cancelled ? "+ future.cancel(false)+ "]");
                
    /**
                 * We can see if the task was canceled.
                 * Returns true if this task was canceled before it completed normally
                 
    */
                System.out.println(
    "Was task canceled before normal complition ? -"+ future.isCancelled());
                
                
            }
    catch(Exception e){
                e.printStackTrace();
            }
    finally{
                executorPool.shutdownNow();
            }
            
        }

        
        
    /*
         *Callable<V> is defined as Callable<String>
         *
         
    */
        
    public static class CallableTask implements Callable<String>{
                    
            
    public String call( ){
                System.out.println(
    "Inside CALLABLE TASK");
                
    return "~~JOVIAL JAVA~~";
            }
        }    
    }
    posted on 2012-08-06 10:28 ゞ沉默是金ゞ 閱讀(780) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 免费无码精品黄AV电影| 免费不卡中文字幕在线| 日韩亚洲国产综合久久久| 国产亚洲婷婷香蕉久久精品| 亚洲免费中文字幕| 男人和女人高潮免费网站| 免费不卡在线观看AV| 国产精品免费看久久久无码| 久久精品国产亚洲AV麻豆~| 亚洲高清一区二区三区电影 | 亚洲欧洲免费无码| 在线看片免费人成视频久网下载| 国产免费av片在线看| 亚洲啪啪综合AV一区| 亚洲日本天堂在线| 男人的天堂网免费网站| 国产美女无遮挡免费视频| 亚洲无线电影官网| 特黄特色的大片观看免费视频| 国产在线观看麻豆91精品免费| 亚洲一区二区三区乱码A| 久久精品国产亚洲av麻豆图片| 99视频免费在线观看| 日韩一区二区在线免费观看 | 四虎影视www四虎免费| 亚洲AV无码专区国产乱码4SE| 亚洲大码熟女在线观看| 曰批全过程免费视频播放网站| 亚洲国产一成久久精品国产成人综合 | 一个人在线观看视频免费| 亚洲中文字幕无码日韩| 亚洲av日韩专区在线观看| 亚洲高清免费在线观看| 日韩一卡2卡3卡4卡新区亚洲 | 在线观看AV片永久免费| 亚洲av午夜成人片精品网站| 黄色网址免费在线| 成人免费视频网址| 亚洲精品视频观看| 在线人成免费视频69国产| 亚洲成aⅴ人片久青草影院|