Truong Giang Dam
Truong Giang Dam

Reputation: 500

How to use synchronized the object in AsyncTask doInBackground()?

I got the trouble with using synchronized the object in background thread. I've specified the problem in sample nutshell:

  1. I write the simple class like that:

    public class UIThreadClass {
    
       MyObject object;
    
       UIThreadClass() {
          object = new MyObject();
    
          object.doActionOne();
          object.doActionTwo();
      }
    }
    
  2. Now my task is improve the code by put two method to non-UI thread. I am using AsyncTask

    For doActionOne()

    private static class DoActionOneTask extends AsyncTask<Void, Void, Void> {
    
        private WeakReference<MyObject> wObject;
    
        DoActionOneTask(MyObject object) {
            wObject = new WeakReference<>(object);
        }
    
        @Override
        protected Void doInBackground(Void... voids) {
           if(wObject.get() != null) {
              MyObject myObject = wObject.get();
              myObject.doActionOne();
           }
    
           return null;
        }
    }
    

    For doActionTwo()

    private static class DoActionTwoTask extends AsyncTask<Void, Void, Void> {
    
        private WeakReference<MyObject> wObject;
    
        DoActionOneTask(MyObject object) {
            wObject = new WeakReference<>(object);
        }
    
        @Override
        protected Void doInBackground(Void... voids) {
           if(wObject.get() != null) {
              MyObject myObject = wObject.get();
              myObject.doActionTwo();
           }
    
           return null;
        }
    }
    

    Now call them in UI thread

    public class UIThreadClass {
    
       MyObject object;
    
       UIThreadClass() {
          object = new MyObject();
    
          new DoActionOneTask(object).execute();
          new DoActionTwoTask(object).execute();
      }
    }
    

The question is:

When using AsyncTask - how to synchronized the object to make sure that the doActionTwo() alway call after doActionOne() method ?.

I have tried using synchronized (myObject) {myObject.doActionOne();} in doInBackground() but the system is warning me that myObject just local variable and it is difficult to guarantee correctness.

Can anyone help me this situation ? Thanks a lot!

Upvotes: 2

Views: 232

Answers (1)

Raj Suvariya
Raj Suvariya

Reputation: 1664

You don't need to do anything. They will execute serially as per the Android docs

Order of execution
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

Upvotes: 2

Related Questions