<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 ゞ沉默是金ゞ 閱讀(774) 評論(0)  編輯  收藏 所屬分類: Java SE
    主站蜘蛛池模板: 91精品手机国产免费| 亚洲AV无码专区亚洲AV伊甸园| 国产免费爽爽视频在线观看| 亚洲欧美第一成人网站7777| 久久久久亚洲AV无码专区体验 | 久久久久亚洲AV无码永不| 国产一级淫片a免费播放口之| 曰批视频免费40分钟试看天天| 中文字幕不卡高清免费| 亚洲AV成人一区二区三区观看 | 1000部国产成人免费视频| 中文字幕不卡高清免费| 黄色毛片免费网站| 亚洲国产成人久久精品大牛影视| 亚洲视频网站在线观看| 亚洲AV无码一区二区三区系列| 亚洲毛片不卡av在线播放一区| 精品剧情v国产在免费线观看| 免费观看AV片在线播放| 中文字幕成人免费视频| 久久大香香蕉国产免费网站| 精品国产呦系列在线观看免费| 麻豆安全免费网址入口| 在线视频亚洲一区| 亚洲爆乳成av人在线视菜奈实| 亚洲人成人网毛片在线播放| 亚洲w码欧洲s码免费| jlzzjlzz亚洲jzjzjz| 亚洲伦理一二三四| 亚洲国产日韩在线成人蜜芽| 亚洲最大在线观看| 亚洲国产综合第一精品小说| 亚洲国产成人手机在线电影bd| 91精品国产亚洲爽啪在线影院| 亚洲国产精久久久久久久| 亚洲AV色香蕉一区二区| 亚洲视频在线一区| 亚洲欧洲日韩综合| 亚洲高清视频在线| 亚洲丶国产丶欧美一区二区三区| 亚洲精品国产首次亮相|