There is a controller's addItem () method, which is used to save an item; Subscriber subscribes to each new item:
public void addItem(String item){ someService.save(item); PublishSubject<String> subject = PublishSubject.create(); subject.subscribeOn(Schedulers.trampoline()).subscribe(simpleObserver); subject.onNext(item); log.info("Save item - " + item); }
and there is a SimpleObserver
public class SimpleObserver extends Subscriber<String> { @Override public void onCompleted() { log.info("onCompleted..."); } @Override public void onError(Throwable throwable) { log.info("onError Subscriber..."); } @Override public void onNext(String s) { log.info("After subscribe " + s"); } }
in a sequential call
addItem1("Item1"); addItem1("Item2"); addItem1("Item3"); addItem1("Item4");
the result is
After subscribe Item1 Save item - Item1 After subscribe Item2 Save item - Item2 After subscribe Item3 Save item - Item3 After subscribe Item4 Save item - Item4
How to make that all someService.save (item) are executed first; in mainThread, and then sequentially in a separate ThreadPool, all onNext () are simpleObserver-a, i.e. the result should be:
Save item - Item1 Save item - Item2 Save item - Item3 Save item - Item4 //все save в mainThread After subscribe Item1 After subscribe Item2 After subscribe Item3 After subscribe Item4 //все After subscribe в одном отдельном пуле потоков последовательно