How come Android's AsyncTask doesn't implement Future?
android, android-asynctask, future, java
Solution
From reading the actual code of `AsyncTask.java` it actually uses a `Future` task and then some more. A `Future` is a task that executes asynchronously on the go. An `AsyncTask` is scheduled on a queue for a single (or pool of) background thread(s).
An `AsyncTask` is actually more "superior" than a `Future` task. It does fancy scheduling and optimizations on top of `Future`'s functionality. Just look at the API introduction levels. `Future` was introduced right from the start API 1.0. The `AsyncTask` object was introduced in API 3.
An AsyncTask has-a Future task, not is-a Future.
AsyncTask.java
/**
* Creates a new asynchronous task. This constructor must be invoked on the UI thread.
*/
public AsyncTask() {
mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true);
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
//noinspection unchecked
return postResult(doInBackground(mParams));
}
};
mFuture = new FutureTask<Result>(mWorker) {
@Override
protected void done() {
try {
postResultIfNotInvoked(get());
} catch (InterruptedException e) {
android.util.Log.w(LOG_TAG, e);
} catch (ExecutionException e) {
throw new RuntimeException("An error occured while executing doInBackground()",
e.getCause());
} catch (CancellationException e) {
postResultIfNotInvoked(null);
}
}
};
}
Problem
In Java, I've gotten used to working with `Futures`. Now I'm looking at Android, and `AsyncTask` implements almost all the same methods and covers similar lifecycles. But, if I want to be consistent and use Future all over my code, I have to wrap AsyncTask in a stupid wrapper, cause it doesn't actually implement Future. All they'd need to add is an `isDone()` method, which seems like it would be trivial, then add `implements Future<Result>`. (added later: see my answer below for just how trivial it would be). Any Android experts know some good reason / obscure bug it might cause why this hasn't been done?