0

I do some network request simultaneously and get the response using HTTPClient in Android. I am using threadsafeclientconnmanager in order to have thread safe connection.

In order to do it simultaneously I am using Java ThreadPool.

private enum STATE { state1, state2, state3 };

ExecutorService executor = Executors.newFixedThreadPool(nThreads);

and submit 3 runnable:

executor.submit(new Processor(state1));
executor.submit(new Processor(state2));
executor.submit(new Processor(state3));

And this Processor class which extends runnable:

class Processor implements Runnable {

    private STATE state;

    public Processor(STATE state) {
        this.state = state;
    }

    @Override
    public void run() {
        switch (state) {
        case state1:
            // Do something
            // Call Back to update UI
            break;
        case state2:
            // Do something
            // Call Back to update UI
            break;
        case state3:
            // Do something
            // Call Back to update UI
            break;
        }
        latch.countDown();
    }
}

As you see in the run method, for every state, I do some instruction and then I need to update UI using callBack methods.

And since that is UI thread every callback method should be :

runOnUiThread(new Runnable() {
     @Override
     public void run() {
        //Update UI
     }
});

Is there any better solution to update UI when we are having Java thread pool in Android?

Ali
  • 9,800
  • 19
  • 72
  • 152
  • What sort of "better" are you looking for? – Jon Skeet Dec 27 '13 at 09:01
  • I am looking for an Android solution based on Android Main UI thread policy. For instance : [Communicating with the UI Thread](https://developer.android.com/training/multiple-threads/communicate-ui.html) – Ali Dec 27 '13 at 10:57
  • AsyncTask is probably what you want, see Miguel's answer. You can execute the AsyncTasks on whatever Executor (your fixed ThreadPool or AsyncTask.THREAD_POOL_EXECUTOR are probably good options) you want and putting your code in the right methods will cause it to execute on the correct thread. – cyngus Dec 27 '13 at 18:05
  • As far as I understood, [there is a limitation for AsyncTask based on core pool size](http://stackoverflow.com/questions/9654148/android-asynctask-threads-limits), and I will not have this limitation when I am using Java thread pool. – Ali Dec 27 '13 at 19:35

1 Answers1

1

You should exec the network connection in other thread, never in main UI thread. When you will have new data, you send it from network thread to main thread.

You can use AsyncTask or Handler.

http://developer.android.com/reference/android/os/AsyncTask.html

https://developer.android.com/training/multiple-threads/communicate-ui.html

Asynctask has "updatePublish" to send data to UI Thread. Handler is a message system to send data to UI Thread and vice versa

I think that Handler is the better option for you.

MiguelAngel_LV
  • 1,200
  • 5
  • 16