Last active
August 20, 2020 08:04
-
-
Save huscael/1cb3b988a2f1f49d690406b3a3bdd2d5 to your computer and use it in GitHub Desktop.
[Rxjava Demo] rxjava #java #rxjava #reactive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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