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?