I study RxJava. There is a main rx construction:
ProcessFactory.generateProcessing(5) .flatMap(processes -> Observable.from(processes)) .flatMap(process -> Observable.just(process)) .doOnNext(process -> { process.start(); }) .subscribe(process -> System.out.println(process.toString() + " subscribed")); The method generates a list of threads wrapped in the Observable:
public static Observable<List<Process>> generateProcessing(int count) { Random rand = new Random(71); int bound = 5; for (int i = 0; i < count; i++) mProcessList.add(Process.newProcess("Process" + i, rand.nextInt(bound))); return Observable.just(mProcessList); } The Process class itself is simple and inherited from Thread , all it does is work for a while and generates some data:
public class Process extends Thread { private String name; private int duration; private int[] myArray; private Process(String name, int duration) { this.name = name; this.duration = duration; Logger.log("Process %s create. d = " + duration, name); } @Override public void run() { Logger.log("Process %s start! d = " + duration + " sec", name); try { Thread.sleep(duration * 1000); } catch (InterruptedException e) { e.printStackTrace(); } myArray = downloadBytes(); Logger.log("Precess %s finish", name); } @Override public String toString() { return name; } private int[] downloadBytes() { int bound = 100; Random rand = new Random(31); return new int[] {rand.nextInt(bound), rand.nextInt(bound), rand.nextInt(bound), rand.nextInt(bound)}; } public int[] getData() { return myArray; } public static Process newProcess(String name, int duration) { return new Process(name, duration); } } How can I make it so that after completing my task, Subscriber can be immediately notified about it and in my case, for example, output data to the console?