content: package com.taobao;
  
  import android.os.Looper;
  import com.taobao.schedulers.AndroidSchedulers;
  import io.reactivex.disposables.Disposable;
  
  import java.util.concurrent.atomic.AtomicBoolean;
  
  /**
   * @author lilong
   * @create 2017-06-05 上午9:53
   */
  
  public abstract class MainThreadDisposable implements Disposable {
      /**
       * Verify that the calling thread is the Android main thread.
       * <p>
       * Calls to this method are usually preconditions for subscription behavior which instances of
       * this class later undo. See the class documentation for an example.
       *
       * @throws IllegalStateException when called from any other thread.
       */
      public static void verifyMainThread() {
          if (Looper.myLooper() != Looper.getMainLooper()) {
              throw new IllegalStateException(
                      "Expected to be called on the main thread but was " + Thread.currentThread().getName());
          }
      }
  
      private final AtomicBoolean unsubscribed = new AtomicBoolean();
  
      @Override
      public final boolean isDisposed() {
          return unsubscribed.get();
      }
  
      @Override
      public final void dispose() {
          if (unsubscribed.compareAndSet(false, true)) {
              if (Looper.myLooper() == Looper.getMainLooper()) {
                  onDispose();
              } else {
                  AndroidSchedulers.mainThread().scheduleDirect(new Runnable() {
                      @Override public void run() {
                          onDispose();
                      }
                  });
              }
          }
      }
  
      protected abstract void onDispose();
  }
  
  