Search code examples
javaandroidmultithreadingthread-synchronization

Android: Task sequential on a thread in JAVA


Have use case where BroadcastReceiver action events received and need execute them in sequential order.

Here as below Event1 and Event2 can be received back to back. Event1 work has to be completed and then Event2 work should be started.

How to make it sequential?

BroadcastReceiver{

onReceive(){

Event1:
Task1 doing Something different.//Cant run network operations here.

Event2:
Task2 doing Something different.//Cant run network operations here.

}

}

executors.newsinglethreadexecutor() or handlerthread is are option i am looking at..any other good way of doing this?


Solution

  • Found that executors.newsinglethreadexecutor() or handlerthread both can handle the task sequential..