Java Code Examples for io.reactivex.Scheduler.Worker#schedule()
The following examples show how to use
io.reactivex.Scheduler.Worker#schedule() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchedulerHelper.java From rxjava2-extras with Apache License 2.0 | 6 votes |
public static void blockUntilWorkFinished(Scheduler scheduler, int numThreads, long timeout, TimeUnit unit) { final CountDownLatch latch = new CountDownLatch(numThreads); for (int i = 1; i <= numThreads; i++) { final Worker worker = scheduler.createWorker(); worker.schedule(new Runnable() { @Override public void run() { worker.dispose(); latch.countDown(); } }); } try { boolean finished = latch.await(timeout, unit); if (!finished) { throw new RuntimeException("timeout occured waiting for work to finish"); } } catch (InterruptedException e) { throw new RuntimeException(e); } }
Example 2
Source File: Processor.java From state-machine with Apache License 2.0 | 6 votes |
private void scheduleSignal(ClassId<?, Id> from, Worker worker, Signal<?, Id> signal, Signal<?, Id> s, long delayMs) { // record pairwise signal so we can cancel it if // desired @SuppressWarnings({ "unchecked", "rawtypes" }) ClassIdPair<Id> idPair = new ClassIdPair<Id>(from, new ClassId(signal.cls(), signal.id())); long t1 = signalScheduler.now(TimeUnit.MILLISECONDS); Disposable subscription = worker.schedule(() -> { subject.onNext(s.now()); } , delayMs, TimeUnit.MILLISECONDS); long t2 = signalScheduler.now(TimeUnit.MILLISECONDS); worker.schedule(() -> { subscriptions.remove(idPair); } , delayMs - (t2 - t1), TimeUnit.MILLISECONDS); Disposable previous = subscriptions.put(idPair, subscription); if (previous != null) { previous.dispose(); } }
Example 3
Source File: MemberSingle.java From rxjava2-jdbc with Apache License 2.0 | 5 votes |
private boolean tryEmit(Observers<T> obs, DecoratingMember<T> m) { // get a fresh worker each time so we jump threads to // break the stack-trace (a long-enough chain of // checkout-checkins could otherwise provoke stack // overflow) // advance counter to the next and choose an Observer to emit to (round robin) int index = obs.index; MemberSingleObserver<T> o = obs.observers[index]; MemberSingleObserver<T> oNext = o; // atomically bump up the index (if that entry has not been deleted in // the meantime by disposal) while (true) { Observers<T> x = observers.get(); if (x.index == index && x.observers[index] == o) { boolean[] active = new boolean[x.active.length]; System.arraycopy(x.active, 0, active, 0, active.length); int nextIndex = (index + 1) % active.length; while (nextIndex != index && !active[nextIndex]) { nextIndex = (nextIndex + 1) % active.length; } active[nextIndex] = false; if (observers.compareAndSet(x, new Observers<T>(x.observers, active, x.activeCount - 1, nextIndex, x.requested - 1))) { oNext = x.observers[nextIndex]; break; } } else { // checkin because no active observers m.checkin(); return false; } } Worker worker = scheduler.createWorker(); worker.schedule(new Emitter<T>(worker, oNext, m)); return true; }
Example 4
Source File: SingleObserveOnRaceTest.java From akarnokd-misc with Apache License 2.0 | 4 votes |
@Test public void race() throws Exception { Worker w = Schedulers.newThread().createWorker(); try { for (int i = 0; i < 1000; i++) { Integer[] value = { 0, 0 }; TestObserver<Integer> to = new TestObserver<Integer>() { @Override public void onSuccess(Integer v) { value[1] = value[0]; super.onSuccess(v); } }; SingleSubject<Integer> subj = SingleSubject.create(); subj.observeOn(Schedulers.single()) .onTerminateDetach() .subscribe(to); AtomicInteger wip = new AtomicInteger(2); CountDownLatch cdl = new CountDownLatch(2); w.schedule(() -> { if (wip.decrementAndGet() != 0) { while (wip.get() != 0); } subj.onSuccess(1); cdl.countDown(); }); Schedulers.single().scheduleDirect(() -> { if (wip.decrementAndGet() != 0) { while (wip.get() != 0); } to.cancel(); value[0] = null; cdl.countDown(); }); cdl.await(); Assert.assertNotNull(value[1]); } } finally { w.dispose(); } }