title: Best practice for eventbus with thread safety
body: <p>My app has activities for the user interaction and a background service which is the only place where the data model is being modified. The background service listens to actions that where made by the user as well as incoming messages from the network. Therefore concurrency issues can arise which I try to prevent by using a handler.
  For the event layer I use greenrobots Eventbus.</p>
  
  <p>This is all working well but I wonder if there is a smarter/faster/less code extensive (and therefore less error prone) way to handle this use case?</p>
  
  <p>To be more specific:</p>
  
  <ul>
  <li>Is there a way to ensure serial execution of the onEvent methods
  without a handler? </li>
  <li>Is there an alternative to having onEvent methods
  for each possible event? </li>
  <li>Is there a better pattern for what I am
  doing here?</li>
  </ul>
  
  <p>This is my approach:</p>
  
  <p>In the oncreate method I do <strong>register the service</strong> (in case of an activity I do this in onstart)</p>
  
  <pre><code>@Override
  public void onCreate() {
      super.onCreate();
      ...
      EventBus.getDefault().register(this);
  }
  </code></pre>
  
  <p>And in the onDestroy I do <strong>unregister</strong> again:</p>
  
  <pre><code>@Override
  public void onDestroy() {
      super.onDestroy();
      ....
      EventBus.getDefault().unregister(this);
  }
  </code></pre>
  
  <p>Whenever I react to an incoming event I want to <strong>ensure serial execution</strong> as there can be concurreny issues because there are incoming events from user interactions as well as from other users via networking. So I decided to work with a handler:</p>
  
  <pre><code>private Handler handler = new Handler(){
          @Override
          public void handleMessage(Message msg) {
              Object receivedEvent = msg.obj;
              if(receivedEvent instanceof EditUser)
              {
                  processEditUserBroadcast((EditUser)receivedEvent);
              }           
              else if(receivedEvent instanceof JoinParty)
              {
                  processJoinPartyBroadcast((JoinParty)receivedEvent);
              }
              else if(receivedEvent instanceof LeaveParty)
              {
                  processLeavePartyBroadcast();
              }
              else if(receivedEvent instanceof SendMessage)
              {
                  processSendMessageBroadcast((SendMessage)receivedEvent);
              }
              else if(receivedEvent instanceof ReceivedMessage)
              {
                  processReceivedMessageBroadcast((ReceivedMessage)receivedEvent);
              }       
              else if(receivedEvent instanceof Reset)
              {
                  processResetBroadcast();
              }
              else if(receivedEvent instanceof ImageDownloadFinished)
              {
                  processImageDownloadFinishedBroadcast((ImageDownloadFinished)receivedEvent);
              }
          }
      };  
      return handler;
  }
  </code></pre>
  
  <p>For each event of interest I do have an onEvent method which is doing nothing but <strong>passing the event to the handler</strong> to ensure serial execution via a small "passToHandler" helper function</p>
  
  <pre><code>public void passToHandler(Handler handler, Object object)
  {
      Message message = handler.obtainMessage();
      message.obj = object;
      handler.sendMessage(message);
  }
  
  public void onEvent(EditUser editUser)
  {
      passToHandler(handler,editUser);
  }
  
  public void onEvent(JoinParty joinParty)
  {
      passToHandler(handler,joinParty);
  }
  
  public void onEvent(LeaveParty leaveParty)
  {
      passToHandler(handler,leaveParty);
  }
  
  public void onEvent(SendMessage sendMessage)
  {
      passToHandler(handler,sendMessage);
  }
  
  public void onEvent(ReceivedMessage receivedMessage)
  {
      passToHandler(handler,receivedMessage);
  }
  
  public void onEvent(Reset reset)
  {
      passToHandler(handler,reset);
  }
  
  public void onEvent(ImageDownloadFinished imageDownloadFinished)
  {
      passToHandler(handler,imageDownloadFinished);
  }
  </code></pre>
  
  <p>The "process.." methods are where the "data magic" happens and shouldn´t be relevant for my question.</p>
  
  <p>And of course for each possible event I did create a class which is usually quite slim like this:</p>
  
  <pre><code>public class JoinParty {
      private String partyCode;
  
      public JoinParty(String partyCode) {
          super();
          this.partyCode = partyCode;
      }
      public String getPartyCode() {
          return partyCode;
      }   
  }
  </code></pre>