<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
    主站蜘蛛池模板: 亚洲色丰满少妇高潮18p| 亚洲国产成人va在线观看网址| 亚洲综合国产成人丁香五月激情| 免费A级毛片无码专区| 四虎精品成人免费视频| 久久精品一区二区免费看| 久久久久久亚洲精品不卡| 四虎一区二区成人免费影院网址 | 在线精品亚洲一区二区三区| 亚洲成A人片在线观看WWW| 久久免费国产精品| 亚洲中文字幕无码久久综合网| 一区二区三区免费高清视频| 精品香蕉在线观看免费| 亚洲国产精品午夜电影| 无码人妻一区二区三区免费 | 亚洲日韩国产一区二区三区在线| 免费专区丝袜脚调教视频| 2020久久精品亚洲热综合一本| 最近2019中文免费字幕| 亚洲高清毛片一区二区| 最近2019免费中文字幕视频三| 亚洲成年看片在线观看| 又粗又长又爽又长黄免费视频| 中文字幕亚洲乱码熟女一区二区| 中文字幕免费不卡二区| 亚洲精品不卡视频| 免费特级黄毛片在线成人观看| 免费一级毛suv好看的国产网站| 亚洲人成无码网站| 16女性下面无遮挡免费| 亚洲国产精品成人午夜在线观看| 国产免费午夜a无码v视频| 国产精品成人免费观看| 亚洲国产成人精品久久| 国产一级高清免费观看| 日韩精品人妻系列无码专区免费| 国产成人精品日本亚洲18图| 亚洲精品高清在线| 免费观看无遮挡www的视频| 精品亚洲福利一区二区|