美文网首页
3、 线程池之submit

3、 线程池之submit

作者: kele2018 | 来源:发表于2020-03-17 13:18 被阅读0次
public Future<?> submit(Runnable task) {
        //任务为空  直接抛空指针异常
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
}

3.1 newTaskFor方法

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

public FutureTask(Runnable runnable, V result) {
        this.callable = Executors.callable(runnable, result); //Callable<V>
        this.state = NEW;       // ensure visibility of callable
}

//弄了个适配器  把传进来的runnable任务转换成了callable任务 这样就可以返回执行结果了 
public static <T> Callable<T> callable(Runnable task, T result) {
        if (task == null)
            throw new NullPointerException();
        return new RunnableAdapter<T>(task, result);
}

 RunnableAdapter(Runnable task, T result) {
            this.task = task;
            this.result = result;
 }

相关文章

网友评论

      本文标题:3、 线程池之submit

      本文链接:https://www.haomeiwen.com/subject/svctyhtx.html