Android AsyncTask API deprecating in Android 11.What are the alternatives?

EDIT: This question is not a duplicate as

  1. Just couple of days back AOSP commit on deprecation was done.
  2. The other question is about using AsyncTaskLoader over AsyncTask.

Google is deprecating Android AsyncTask API in Android 11 and suggesting to use java.util.concurrent instead. you can check out the commit here

 * @deprecated Use the standard <code>java.util.concurrent</code> or
 *   <a href="">
 *   Kotlin concurrency utilities</a> instead.
public abstract class AsyncTask<Params, Progress, Result> {

If you’re maintaining an older codebase with asynchronous tasks in Android, you’re likely going to have to change it in future. My question is that what should be proper replacement of the code snippet shown below using java.util.concurrent. It is a static inner class of an Activity. I am looking for something that will work with minSdkVersion 16

private static class LongRunningTask extends AsyncTask<String, Void, MyPojo> {
        private static final String TAG = MyActivity.LongRunningTask.class.getSimpleName();
        private WeakReference<MyActivity> activityReference;

        LongRunningTask(MyActivity context) {
            activityReference = new WeakReference<>(context);

        protected MyPojo doInBackground(String... params) {
            // Some long running task


        protected void onPostExecute(MyPojo data) {

            MyActivity activity = activityReference.get();
            populateData(activity, data) ;


1 answer

  • answered 2019-11-08 13:55 EpicPandaForce

    private WeakReference<MyActivity> activityReference;

    Good riddance that it's deprecated, because the WeakReference<Context> was always a hack, and not a proper solution.

    Now people will have the opportunity to sanitize their code.

    AsyncTask<String, Void, MyPojo> 

    Based on this code, Progress is actually not needed, and there is a String input + MyPojo output.

    This is actually quite easy to accomplish without any use of AsyncTask.

    public class TaskRunner {
        private final Executor executor = Executors.newSingleThreadExecutor(); // change according to your requirements
        private final Handler handler = new Handler(Looper.getMainLooper());
        public interface Callback<R> {
            void onComplete(R result);
        public <R> void executeAsync(Callable<R> callable, Callback<R> callback) {
            executor.execute(() -> {
                final R result =;
       -> {

    How to pass in the String? Like so:

    class LongRunningTask implements Callable<MyPojo> {
        private final String input;
        public LongRunningTask(String input) {
            this.input = input;
        public MyPojo call() {
            // Some long running task
            return myPojo;


    // in ViewModel
    taskRunner.executeAsync(new LongRunningTask(input), (data) -> {
        // MyActivity activity = activityReference.get();
        // activity.progressBar.setVisibility(View.GONE);
        // populateData(activity, data) ;
    // in Activity
    protected void onCreate(Bundle savedInstanceState) {
        viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
        viewModel.loadingLiveData.observe(this, (loading) -> {
            if(loading) {
            } else {
        viewModel.dataLiveData.observe(this, (data) -> {

    This example used a single-threaded pool which is good for DB writes (or serialized network requests), but if you want something for DB reads or multiple requests, you can consider the following Executor configuration:

    private static final Executor THREAD_POOL_EXECUTOR =
            new ThreadPoolExecutor(5, 128, 1,
                    TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());