I am learning RxJava and created code
Using this class to create a list of random Ints
public class Sources {
public List<Integer> randomIntegerList() throws InterruptedException {
TimeUnit.SECONDS.sleep(4);
return IntStream
.range(0, 99)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
}
}
Some random functions
public class Methods {
public boolean isEven(Integer integer) {
if (integer % 2 == 0) {
return true;
}
return false;
}
}
Problem is here
public class DemoApplication {
public static void main(String[] args) {
new DemoApplication().executeSingle();
}
private void executeSingle() {
Single.create(emitter -> {
List<Integer> list = null;
try {
list = new Sources()
.randomIntegerList();
} catch (InterruptedException e) {
emitter.onError(e);
}
emitter.onSuccess(list);
})
.subscribe(
x -> {printData((List<Integer>) x);}
);
System.out.println("Finished");
}
private void printData(List<Integer> list) {
list .stream()
.filter(y -> new Methods().isEven(y))
.forEach(System.out::println);
}
}
Finished is printed after a delay of 4 seconds.. basically after subscribe executes its functionality, how can I make it non-blocking. what I want is that thread keep executing next lines and when printData() function is called that thread stop executing and execute printData() first.