Search in sources :

Example 1 with SchedulerMultiWorkerSupport

use of io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport in project RxJava by ReactiveX.

the class ParallelRunOn method subscribe.

@Override
public void subscribe(Subscriber<? super T>[] subscribers) {
    subscribers = RxJavaPlugins.onSubscribe(this, subscribers);
    if (!validate(subscribers)) {
        return;
    }
    int n = subscribers.length;
    @SuppressWarnings("unchecked") final Subscriber<T>[] parents = new Subscriber[n];
    if (scheduler instanceof SchedulerMultiWorkerSupport) {
        SchedulerMultiWorkerSupport multiworker = (SchedulerMultiWorkerSupport) scheduler;
        multiworker.createWorkers(n, new MultiWorkerCallback(subscribers, parents));
    } else {
        for (int i = 0; i < n; i++) {
            createSubscriber(i, subscribers, parents, scheduler.createWorker());
        }
    }
    source.subscribe(parents);
}
Also used : ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) SchedulerMultiWorkerSupport(io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport)

Example 2 with SchedulerMultiWorkerSupport

use of io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport in project RxJava by ReactiveX.

the class SchedulerMultiWorkerSupportTest method moreThanMaxWorkers.

@Test
public void moreThanMaxWorkers() {
    final List<Worker> list = new ArrayList<>();
    SchedulerMultiWorkerSupport mws = (SchedulerMultiWorkerSupport) Schedulers.computation();
    mws.createWorkers(max * 2, new WorkerCallback() {

        @Override
        public void onWorker(int i, Worker w) {
            list.add(w);
        }
    });
    assertEquals(max * 2, list.size());
}
Also used : WorkerCallback(io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 3 with SchedulerMultiWorkerSupport

use of io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport in project RxJava by ReactiveX.

the class SchedulerMultiWorkerSupportTest method distinctThreads.

@Test
public void distinctThreads() throws Exception {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final CompositeDisposable composite = new CompositeDisposable();
        try {
            final CountDownLatch cdl = new CountDownLatch(max * 2);
            final Set<String> threads1 = Collections.synchronizedSet(new HashSet<>());
            final Set<String> threads2 = Collections.synchronizedSet(new HashSet<>());
            Runnable parallel1 = new Runnable() {

                @Override
                public void run() {
                    final List<Worker> list1 = new ArrayList<>();
                    SchedulerMultiWorkerSupport mws = (SchedulerMultiWorkerSupport) Schedulers.computation();
                    mws.createWorkers(max, new WorkerCallback() {

                        @Override
                        public void onWorker(int i, Worker w) {
                            list1.add(w);
                            composite.add(w);
                        }
                    });
                    Runnable run = new Runnable() {

                        @Override
                        public void run() {
                            threads1.add(Thread.currentThread().getName());
                            cdl.countDown();
                        }
                    };
                    for (Worker w : list1) {
                        w.schedule(run);
                    }
                }
            };
            Runnable parallel2 = new Runnable() {

                @Override
                public void run() {
                    final List<Worker> list2 = new ArrayList<>();
                    SchedulerMultiWorkerSupport mws = (SchedulerMultiWorkerSupport) Schedulers.computation();
                    mws.createWorkers(max, new WorkerCallback() {

                        @Override
                        public void onWorker(int i, Worker w) {
                            list2.add(w);
                            composite.add(w);
                        }
                    });
                    Runnable run = new Runnable() {

                        @Override
                        public void run() {
                            threads2.add(Thread.currentThread().getName());
                            cdl.countDown();
                        }
                    };
                    for (Worker w : list2) {
                        w.schedule(run);
                    }
                }
            };
            TestHelper.race(parallel1, parallel2);
            assertTrue(cdl.await(5, TimeUnit.SECONDS));
            assertEquals(threads1.toString(), max, threads1.size());
            assertEquals(threads2.toString(), max, threads2.size());
        } finally {
            composite.dispose();
        }
    }
}
Also used : WorkerCallback(io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Aggregations

RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)2 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)2 WorkerCallback (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback)2 Test (org.junit.Test)2 CompositeDisposable (io.reactivex.rxjava3.disposables.CompositeDisposable)1 SchedulerMultiWorkerSupport (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport)1 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)1