CancellableDisposable extends AtomicReference<Cancellable> implements Disposable { @Override public void dispose() { if (get() != null) { Cancellable c = getAndSet(null); if (c != null) { try { c.cancel(); } catch (Exception ex) { Exceptions.throwIfFatal(ex); RxJavaPlugins.onError(ex); } } } } CancellableDisposable(Cancellable cancellable); @Override boolean isDisposed(); @Override void dispose();  }
@Test public void disposeRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final AtomicInteger count = new AtomicInteger(); Cancellable c = new Cancellable() { @Override public void cancel() throws Exception { count.getAndIncrement(); } }; final CancellableDisposable cd = new CancellableDisposable(c); Runnable r = new Runnable() { @Override public void run() { cd.dispose(); } }; TestHelper.race(r, r); assertEquals(1, count.get()); } }