-
-
Save mmynsted/b11fa186023a623f92186b44fcce511f to your computer and use it in GitHub Desktop.
Simple looping Observable with sleep interval and timeout. Actually getting an item would replace the line with i++
This file contains 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.Observable; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* Created by mmynsted on 2/8/17. | |
*/ | |
public class ShowItem { | |
public void printItem() { | |
int timeoutSecs = 5; | |
int sleepTime = 1000; | |
//create an observable that keeps trying to get an item | |
Observable<Integer> x = Observable.fromCallable(() -> { | |
int i = 0; | |
while (!isValid(i)) { //testing the item | |
Thread.sleep(sleepTime); | |
i++; //getting the item | |
} | |
return i; | |
}).timeout(timeoutSecs, TimeUnit.SECONDS); | |
//show it | |
x.subscribe(System.out::println); | |
} | |
private static boolean isValid(int in) { | |
return (in > 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment