Skip to content Skip to sidebar Skip to footer

Syncronizing Tasks In Android

In my application I have multiple places where AsyncTask can be invoked to connect to web, download stuff, etc. It can be from Activity or from background service. There is various

Solution 1:

It might be easier to just use an operation queue backed by a single worker thread. That way all tasks will be run in the order they are received. You can easily accomplish this by using the Executors factory methods. Here's a simple example:

private static final ExecutorService mThreadPool =
        Executors.newSingleThreadExecutor();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mThreadPool.execute(new Runnable() {
        public void run() {
            // Do some work here...
        }
    });

    mThreadPool.execute(new Runnable() {
        public void run() {
            // Do some additional work here after
            // the first Runnable has finished...
        }
    });
}

Post a Comment for "Syncronizing Tasks In Android"