/**
   * Unsubscribe the given subscription id.
   *
   * @see #subscribe(Map, Entity, Sensor, SensorEventListener)
   */
  public synchronized boolean unsubscribe(SubscriptionHandle sh) {
    if (!(sh instanceof Subscription))
      throw new IllegalArgumentException(
          "Only subscription handles of type Subscription supported: sh="
              + sh
              + "; type="
              + (sh != null ? sh.getClass().getCanonicalName() : null));
    Subscription s = (Subscription) sh;
    boolean b1 = allSubscriptions.remove(s.id) != null;
    boolean b2 =
        LanguageUtils.removeFromMapOfCollections(
            subscriptionsByToken, makeEntitySensorToken(s.producer, s.sensor), s);
    assert b1 == b2;
    if (s.subscriber != null) {
      boolean b3 =
          LanguageUtils.removeFromMapOfCollections(subscriptionsBySubscriber, s.subscriber, s);
      assert b3 == b2;
    }

    // TODO Requires code review: why did we previously do exactly same check twice in a row (with
    // no synchronization in between)?
    if ((subscriptionsBySubscriber.size() == 0
            || !groovyTruth(subscriptionsBySubscriber.get(s.subscriber)))
        && !s.subscriberExecutionManagerTagSupplied
        && s.subscriberExecutionManagerTag != null) {
      // if subscriber has gone away forget about his task; but check in synch block to ensure
      // setTaskPreprocessor call above will win in any race
      if ((subscriptionsBySubscriber.size() == 0
          || !groovyTruth(subscriptionsBySubscriber.get(s.subscriber))))
        ((BasicExecutionManager) em).clearTaskPreprocessorForTag(s.subscriberExecutionManagerTag);
    }

    // FIXME ALEX - this seems wrong
    ((BasicExecutionManager) em)
        .setTaskSchedulerForTag(s.subscriberExecutionManagerTag, SingleThreadedScheduler.class);
    return b1;
  }