Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:10:51+00:00 2026-06-01T04:10:51+00:00

Is it possible to make AsyncTask.doInBackground synchronized – or achieve the same result in

  • 0

Is it possible to make AsyncTask.doInBackground synchronized – or achieve the same result in another way?

class SynchronizedTask extends AsyncTask {

    @Override
    protected synchronized Integer doInBackground(Object... params) {
        // do something that needs to be completed 
        // before another doInBackground can be called
    }
}

In my case, any AsyncTask.execute() can be started before a previous one has completed, but I need to execute the code in doInBackground only after the previous task has finished.

EDIT: As correctly pointed out, the synchronization works only on the same object instance.
Unfortunately, it is not possible to create an AsyncTask and call execute() more than once on the same object instance, as specified in the “Threading rules” section of the AsyncTask documentation.

The solution is to use a custom Executor to serialize the tasks, or, if you use API 11 or above, AsyncTask.executeOnExecutor(), as suggested in the comments below.

I posted an answer showing an implementation of a SerialExecutor that can be used to queue tasks that will be executed sequentially.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:10:53+00:00Added an answer on June 1, 2026 at 4:10 am

    Ideally, I’d like to be able to use AsyncTask.executeOnExecutor() with a SERIAL_EXECUTOR, but this is only available for API level 11 or above:

    new AsyncTask().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, params);
    

    To target the Android APIs below level 11, I ended up implementing a custom class which encapsulates an ExecutorService with a thread pool size of 1. The full code is open-sourced here.

    Executors.newFixedThreadPool(int nThreads) creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. In my case, nThreads is 1, which means tasks can be queued, but only one task will be executed at any given time.

    Here is the code:

    public abstract class SerialExecutor {
        private final ExecutorService mExecutorService;
    
        public SerialExecutor() {
            mExecutorService = Executors.newFixedThreadPool(1);
        }
    
        public void queue(Context context, TaskParams params) {
            mExecutorService.submit(new SerialTask(context, params));
        }
    
        public void stop() {
            mExecutorService.shutdown();
        }
    
        public abstract void execute(TaskParams params);
    
        public static abstract class TaskParams { }
    
        private class SerialTask implements Runnable {
            private final Context mContext;
            private final TaskParams mParams;
    
            public SerialTask(Context context, TaskParams params) {
                mContext = context;
                mParams = params;
            }
    
            public void run() {
                execute(mParams);
                Activity a = (Activity) mContext;
                a.runOnUiThread(new OnPostExecute());
            }
        }
    
        /**
         * Used to notify the UI thread
         */
        private class OnPostExecute implements Runnable {
    
            public void run() {
    
            }
        }
    }
    

    This can be extended and used as a serial task executor in an Activity:

    public class MyActivity extends Activity {
        private MySerialExecutor mSerialExecutor;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // ...
            mSerialExecutor = new MySerialExecutor();
        }
    
        @Override
        protected void onDestroy() {
            if (mSerialExecutor != null) {
                mSerialExecutor.stop();
            }
            super.onDestroy();
        }
    
        public void onTrigger(int param) {
            mSerialExecutor.queue(this, new MySerialExecutor.MyParams(param));
        }
    
        private static class MySerialExecutor extends SerialExecutor {
    
            public MySerialExecutor() {
                super();
            }
    
            @Override
            public void execute(TaskParams params) {
                MyParams myParams = (MyParams) params;
                // do something...
            }
    
            public static class MyParams extends TaskParams {
                // ... params definition
    
                public MyParams(int param) {
                    // ... params init
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to make a circular JFrame without the need to the Class
Is it possible make some handler that will do something when user shutdown computer
Is it possible make an activity, that will lock the android device? That device
Possible Duplicate: Make iPhone app paid version replace free version on install from app
is possible to make own help message or attach own event on help option
How is it possible to make prototype methods in C#.Net? In JavaScript, I can
Is it possible to make a site with ASP.NET MVC Framework using .NET 2.0?
Is it possible to make it appear to a system that a key was
Is it possible to make such buttons ( http://img225.imageshack.us/img225/6452/buttonslw9.jpg ) using CSS? It should
Is it possible to make efficient queries that use the complete regular expression feature

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.