Reputation:
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.
I understand the RxJava equivalent of
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
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Upvotes: 1
Views: 215
Reputation: 1199
This question is answered very well by the "Scheduler" section of the Rx Java documentation, located here. I will not attempt to summarise that wealth of information here suffice to say that the equivalent of 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
.
Upvotes: 2