what is the advantage of using FutureTask over Callable?

前端 未结 3 1496
野趣味
野趣味 2020-12-25 13:15

There are two approaches to submitting and polling task for result

FutureTask futureTask = new FutureTask(callable);
3条回答
  •  囚心锁ツ
    2020-12-25 14:03

    Almost certainly none at all. A quick browse on GrepCode of the AbstractExecutorService shows each of these methods are simply helper methods that ultimately wrap the Callable/Runnable in a Future for you.

    protected  RunnableFuture newTaskFor(Runnable runnable, T value) {
        return new FutureTask(runnable, value);
    }
    
    protected  RunnableFuture newTaskFor(Callable callable) {
        return new FutureTask(callable);
    }
    
    public Future submit(Runnable task) {
        // ...
        RunnableFuture ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }
    
    public  Future submit(Runnable task, T result) {
        // ...
        RunnableFuture ftask = newTaskFor(task, result);
        execute(ftask);
        return ftask;
    }
    
    public  Future submit(Callable task) {
        // ...
        RunnableFuture ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }
    
        

    提交回复
    热议问题