Skip to content

Instantly share code, notes, and snippets.

@huscael
Last active August 20, 2020 08:04
Show Gist options
  • Save huscael/1cb3b988a2f1f49d690406b3a3bdd2d5 to your computer and use it in GitHub Desktop.
Save huscael/1cb3b988a2f1f49d690406b3a3bdd2d5 to your computer and use it in GitHub Desktop.
[Rxjava Demo] rxjava #java #rxjava #reactive
import io.reactivex.rxjava3.core.Observable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static ExecutorService pool = Executors.newFixedThreadPool(1);
public static Observable<Integer> createObservable() {
return Observable.create(sub -> {
pool.submit(() -> {
int i = 0;
while (true) {
try {
sub.onNext(i);
i += 1;
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
});
}
public static void main(String[] args) {
Main.createObservable()
.skip(2)
.take(5)
.subscribe(System.out::println);
pool.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment