共同学习Java源代码-多线程与并发-AbstractExecutorService类(一)

来源:互联网 发布:广发淘宝卡 集分宝2017 编辑:程序博客网 时间:2024/06/09 19:55

public abstract class AbstractExecutorService implements ExecutorService

这个类实现了ExecutorService接口


    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
        return new FutureTask<T>(runnable, value);
    }


    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
        return new FutureTask<T>(callable);
    }

这两个方法是创建新任务的方法 将参数传给new出来的FutureTask



    public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }


    public <T> Future<T> submit(Runnable task, T result) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task, result);
        execute(ftask);
        return ftask;
    }


    public <T> Future<T> submit(Callable<T> task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }

这三个方法是将任务提交给线程池的方法 

判断任务为空就抛出异常 

调用上面的方法创建一个新的FutureTask并赋给接口RunnableFuture 

执行RunnableFuture 返回执行完的RunnableFuture



阅读全文
0 0
原创粉丝点击