ArrayCompositeDisposable extends AtomicReferenceArray<Disposable> implements Disposable { public Disposable replaceResource(int index, Disposable resource) { for (;;) { Disposable o = get(index); if (o == DisposableHelper.DISPOSED) { resource.dispose(); return null; } if (compareAndSet(index, o, resource)) { return o; } } } ArrayCompositeDisposable(int capacity); boolean setResource(int index, Disposable resource); Disposable replaceResource(int index, Disposable resource); @Override void dispose(); @Override boolean isDisposed();  }
@Test public void replaceRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final ArrayCompositeDisposable acd = new ArrayCompositeDisposable(2); Runnable r = new Runnable() { @Override public void run() { acd.replaceResource(0, Disposables.empty()); } }; TestHelper.race(r, r); } }