简体   繁体   English

有没有一种方法可以使用RxJava在多个线程上运行方法

[英]Is there a way to have methods run on multiple threads using RxJava

I'm new to RxJava and I'm trying to figure out if there is an equivalent to running multiple async tasks on various threads in parallel. 我是RxJava的新手,我想弄清楚是否等效于在多个线程上并行运行多个异步任务。

I understand the RxJava equivalent of 我了解RxJava的等效

AsyncTask asyncTask = new AsyncTask<String, Void, ObjectType>(){
    @Override
    protected ObjectType doInBackground(String... params) {
         return someMethod(params[0]);
    }

    @Override
    protected void onPostExecute(ObjectType objectType) {}
}

aynctask.execute();

is

Observable.just(string)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .map(this::someMethod)
    .subscribe(new Observer<ObjectType>() {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {}

        @Override
        public void onNext(ObjectType objectType) {}
    });

But how can I implement in RxJava the equivalent of 但是我该如何在RxJava中实现相当于

asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

This question is answered very well by the "Scheduler" section of the Rx Java documentation, located here . 这个问题是由RX Java文档的“计划”部分,位于回答非常好这里 I will not attempt to summarise that wealth of information here suffice to say that the equivalent of asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 我不会尝试在此总结大量信息,足以说asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);等效asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); is to get the operators in your cascade of Observable operators to operate on particular schedulers. 是让可观察运算符级联中的运算符在特定调度程序上进行运算。

In particular, you can use the Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR) method to execute the operator on a particular java.util.concurrent.Executor . 特别是,您可以使用Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR)方法在特定的java.util.concurrent.Executor上执行运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM