FutureTask

参考资料

深入学习FutureTask       http://www.importnew.com/25286.html

FutureTask应用实例

package com.person;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class FuturnTaskTest {
    /**
     *任务类 :实现Callable,同时可以定义返回值类型
     */
    class CallableTask implements Callable<String>{
        @Override
        public String call() throws Exception {
            String msg = "CallableTask 执行中";
            System.out.println(msg);
            return "CallableTask Done";
        }
        
    }
    
    /**
     * 任务类 :实现Runnable
     */
    class RunnableTask implements Runnable{

        @Override
        public void run() {
            String msg = "RunnableTask 执行中";
            System.out.println(msg);
            try {
                Thread.sleep(10000); //10秒钟
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    
    public static void main(String[] args) throws Exception{
        /*构造方法1使用
         * 
         */
        CallableTask callableTask = new FuturnTaskTest().new CallableTask();
        FutureTask<String> futureTask = new FutureTask<String>(callableTask);
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
        
        /*构造方法2使用
         * 这个构造函数会把传入的Runnable封装成一个Callable对象保存在callable字段中,
         * 同时如果任务执行成功的话就会返回传入的result。这种情况下如果不需要返回值的话可以传入一个null
         */
        RunnableTask runableTask = new FuturnTaskTest().new RunnableTask();
        String result ="RunnableTask Done";
        FutureTask<String> runableFutureTask = new FutureTask<String>(runableTask,result); //用到了适配器转换模式
        new Thread(runableFutureTask).start();
        System.out.println(runableFutureTask.get());
        
        /*
         * FutureTask主要方法,重点关注FutureTask源码实现
         */
        futureTask.isDone();
        futureTask.cancel(true); //true表示如果在执行中,则打断,实际只是执行了线程的interrupt()方法,设置了一个线程中断标志位
        futureTask.isCancelled();
        futureTask.get();
        futureTask.get(1,TimeUnit.MINUTES);
        
    }

}

FutureTask七种状态以及可能变化的四种过程

    /* Possible state transitions:
     * NEW -> COMPLETING -> NORMAL        当Callable或Runnable中的run方法执行完成之前,状态都是NEW
     * NEW -> COMPLETING -> EXCEPTIONAL
     * NEW ->               CANCELLED
     * NEW -> INTERRUPTING -> INTERRUPTED
     */
    private volatile int state;
    private static final int NEW          = 0;
    private static final int COMPLETING   = 1;
    private static final int NORMAL       = 2;
    private static final int EXCEPTIONAL  = 3;
    private static final int CANCELLED    = 4;
    private static final int INTERRUPTING = 5;
    private static final int INTERRUPTED  = 6;
    
    private Callable<V> callable; //保存创建对象时传入的Callable对象
    private Object outcome;  //保存任务执行结果或者异常原因。
    private volatile Thread runner;    
    private volatile WaitNode waiters;

 

posted @ 2018-09-04 08:01  娃力先生  阅读(153)  评论(0编辑  收藏  举报