0

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?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74
  • 1
    Why not put it into one `Thread`? – Murat Karagöz May 08 '17 at 13:45
  • Save a reference of Action2 in Action1 and as last command in Action1 schedule Action2. Then everything runs sequential and you don't have to care how many threads are there. – Robert May 08 '17 at 13:49
  • 1
    The easiest way to do this is to serialize them by sending a message to a common thread and running the handlers on that common thread. That ensures one finished before the other. Another way would be to use semaphores to serialize access to the critical sections of the two handlers. Either way, you need to be aware of the possibility of action 2 occurring before action 1 and handling that case. – Gabe Sechan May 08 '17 at 13:58
  • @MuratK. edited my Question. – NitZRobotKoder May 08 '17 at 15:22

2 Answers2

0

i have a sollution But i dont know if there's much better sollution than this or not

well at least it works

use Asyntask with your Events

public class Event1 extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... params) {
        //use your Event 1 methode here
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        //call Event2 
        Event2 event2=new Event2();
        event2.execute();
    }
}
    public class Event2 extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... params) {
            //use your Event 2 methode here
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //call Event3
            Event3 event3=new Event3();
            event3.execute();
        }
    }

public class Event3 extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... params) {
            //use your Event3 methode here
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //everything would come after Event 3 write it here
        }
    }
NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74
Farido mastr
  • 464
  • 5
  • 12
0

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

NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74