Skip to content

Instantly share code, notes, and snippets.

@mmynsted
Last active February 9, 2017 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmynsted/b11fa186023a623f92186b44fcce511f to your computer and use it in GitHub Desktop.
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++
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