title: IntentService Concurrency
body: <p>I have got a class that extends IntentService, let's say class A, and then I have got 2 more classes as class B and class C, these classes extends class A. The reason is because I want to initialize everything in class A so that code would be reused in class B and C. So class B and C's <code>onHandleIntent</code> method looks like:</p>
  
  <pre><code>@Override
  protected void onHandleIntent(Intent intent) {
      super.onHandleIntent(intent);
              //Specific init stuff
  }
  </code></pre>
  
  <p>What I want to do is simply do some concurrency checks at class A's either constructor or <code>onHandleIntent</code>. For example when method x is being used(doesn't matter which class uses it) I would like to be able to flag this method being used. Sometimes one method schedules another thread by using <code>Handler.postDelayed</code>, so I want to make sure that flag is kept as in use until the thead work is finished.</p>
  
  <p>So far it sounds like it can be done easily by shared singleton, however each of these classes have their own <code>AlarmManager</code> which extends <code>BroadcastReceiver</code>, so using a shared singleton doesn't really work as lifecycle existence of a singleton is not guaranteed.</p>
  
  <p>The only solution that I can think of is using a database or local file system, which sounds quite silly. So is there another way to flag concurrent methods among different <code>IntentService</code> that are being triggered by <code>AlarmManager</code>?</p>
  
  <p>To give a clear image I'd like to give an example of class A, class B and class C</p>
  
  <pre><code>public class A extends IntentService {
      public A(String name) {
          super(name);
          //initialise objects here
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          //initialise objects that uses intent here
      }
  
      protected synchronized void methodA() {
          //wait until flagB is free
          flagA = in_use; //Flag something as being in use here
          //change some objects here
          Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
          @Override
          public void run() {
              //do some stuff with objects
                      flagB = not_in_use; //Flag something as being free here
          }
          }, (1000 * 45));// 45secs
      }
  
      protected synchronized void methodB() {
          if (flagA == not_in_use) {
              flagB = in_use;
               //do some stuff with objects
              flagB = not_in_use;   
          }
          else {
               //do something else
          }
  }
  
  public class B extends IntentService {
      public A(String name) {
          super("B");//Super initializes everything
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {//This will run every 30 minutes
          super.onHandleIntent(intent);
          methodA();
      }
  }
  
  
  public class C extends IntentService {
      public A(String name) {
          super("C");//Super initializes everything
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {//This will run every 45 minutes
          super.onHandleIntent(intent);
          methodB();
      }
  }
  </code></pre>
  
  <p>So basically class B and C doesn't contain any private classes. What i want to do is just like the example above, flag things.
  The problem is as I mentioned if it is just a static field, then static field might not stay in the memory as Android OS can kill anything inactive for saving memory.</p>