title: Android: In a multi-threaded Service, is LocalBroadcastManager safe?
body: <p>In a multi-threaded Service is <code>LocalBroadcastManager.getInstance(this).sendBroadcast()</code>, called from different threads, already thread-safe?</p>
  
  <pre><code>public class MyService extends Service
  {
      // [...] define binder to allow access to doSomething() and doSomethingElse(), etc.
  
      @Override
      public IBinder onBind(Intent intent) 
      {
          return binder;
      }
  
      public void doSomething() 
      {               
          new Thread(new Runnable() 
          {           
               public void run() 
               {
                  // [...] do something
  
                  // TODO is this threadsafe??
                  LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
                  broadcastManager.sendBroadcast(new Intent(DONE_SOMETHING));
              }
          }).start();
      }
  
      public void doSomethingElse()
      {               
           new Thread(new Runnable() 
           {          
               public void run() 
               {
                   // [...] do something else
  
                   // TODO is this threadsafe??
                   LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
                   broadcastManager.sendBroadcast(new Intent(DONE_SOMETHING_ELSE));
              }
          }).start();
      }
  }
  </code></pre>
  
  <p>Or <code>LocalBroadcastManager.getInstance(this)</code> returns actually always the same instance for the same context and is not otherwise synchronized, so I actually need to add a level of synchronization around the <code>LocalBroadcastManager</code>, with something like:</p>
  
  <pre><code>MyService extends Service
  {
      private class SyncedLocalBroadcastManager
      {       
          private LocalBroadcastManager broadcastManager;
  
          SyncedLocalBroadcastManager(LocalBroadcastManager manager)
          {
              broadcastManager = manager;
          }
  
          public synchronized void send(Intent intent)
          {
              broadcastManager.sendBroadcast(intent);
          }
      }
  
      private SyncedLocalBroadcastManager syncedBroadcastManager; 
  
      // [...] define binder to allow access to doSomething() and doSomethingElse(), etc.
  
      @Override
      public IBinder onBind(Intent intent) 
      {
          syncedBroadcastManager = new SyncedLocalBroadcastManager(LocalBroadcastManager.getInstance(this));
  
          return binder;
      }
  
      public void doSomething() 
      {               
          new Thread(new Runnable() 
          {           
               public void run() 
               {
                  // [...] do something
  
                  syncedBroadcastManager.send(new Intent(DONE_SOMETHING));
              }
          }).start();
      }
  
      public void doSomethingElse()
      {               
           new Thread(new Runnable() 
           {          
               public void run() 
               {
                   // [...] do something else
  
                   syncedBroadcastManager.send(new Intent(DONE_SOMETHING_ELSE));
              }
          }).start();
      }
  
  }
  </code></pre>
  
  <p>Or is there any other preferred/canonical way? </p>
  
  <p>I wouldn't want to add unnecessary synchronization in an already complex structure, but I cannot find any confirmation.</p>