Java Code Examples for io.reactivex.Observable.interval()

The following are Jave code examples for showing how to use interval() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Learning-RxJava   File: Ch2_35.java   View Source Code Vote up 7 votes
public static void main(String[] args) {
        Observable<Long> seconds =
                Observable.interval(1, TimeUnit.SECONDS);
//subscribe and capture disposables
        Disposable disposable1 =
                seconds.subscribe(l -> System.out.println("Observer 1: " +
                        l));
        Disposable disposable2 =
                seconds.subscribe(l -> System.out.println("Observer 2: " +
                        l));
//put both disposables into CompositeDisposable
        disposables.addAll(disposable1, disposable2);
//sleep 5 seconds
        sleep(5000);
//dispose all disposables
        disposables.dispose();
//sleep 5 seconds to prove
//there are no more emissions
        sleep(5000);
    }
 
Example 2
Project: Learning-RxJava   File: Ch2_34.java   View Source Code Vote up 7 votes
public static void main(String[] args) {
        Observable<Long> source =
                Observable.interval(1, TimeUnit.SECONDS);
        ResourceObserver<Long> myObserver = new
                ResourceObserver<Long>() {
                    @Override
                    public void onNext(Long value) {
                        System.out.println(value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("Done!");
                    }
                };
//capture Disposable
        Disposable disposable = source.subscribeWith(myObserver);
    }
 
Example 3
Project: Learning-RxJava   File: Ch4_14.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Observable<String> strings =
            Observable.just("Alpha", "Beta", "Gamma", "Delta",
                    "Epsilon");
    Observable<Long> seconds =
            Observable.interval(1, TimeUnit.SECONDS);
    Observable.zip(strings, seconds, (s, l) -> s)
            .subscribe(s ->
                    System.out.println("Received " + s +
                            " at " + LocalTime.now())
            );
    sleep(6000);
}
 
Example 4
Project: Learning-RxJava   File: Ch7_7.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Observable<Long> cutOffs =
            Observable.interval(1, TimeUnit.SECONDS);
    Observable.interval(300, TimeUnit.MILLISECONDS)
            .map(i -> (i + 1) * 300) // map to elapsed time
            .buffer(cutOffs)
            .subscribe(System.out::println);
    sleep(5000);
}
 
Example 5
Project: Phoenix-for-VK   File: LongpollService.java   View Source Code Vote up 6 votes
@Override
public void onCreate() {
    super.onCreate();
    this.mRegisteredPeers = new SparseArray<>(2);
    this.mActiveLongpolls = new SparseArray<>(1);

    Logger.d(TAG, "Service created");

    mCompositeDisposable.add(Settings.get()
            .accounts()
            .observeChanges()
            .observeOn(Injection.provideMainThreadScheduler())
            .subscribe(this::onAccountChange));

    mShutdownObservable = Observable.interval(INTERVAL, INTERVAL, TimeUnit.MILLISECONDS,
            Injection.provideMainThreadScheduler());
    restartShutdownDelay();
}
 
Example 6
Project: Learning-RxJava   File: Ch4_16.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
    Observable<Long> source1 =
            Observable.interval(300, TimeUnit.MILLISECONDS);
    Observable<Long> source2 =
            Observable.interval(1, TimeUnit.SECONDS);
    source2.withLatestFrom(source1,
            (l1, l2) -> "SOURCE 2: " + l1 + " SOURCE 1: " + l2
    ).subscribe(System.out::println);
    sleep(3000);
}
 
Example 7
Project: Learning-RxJava   File: Ch4_15.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
    Observable<Long> source1 =
            Observable.interval(300, TimeUnit.MILLISECONDS);
    Observable<Long> source2 =
            Observable.interval(1, TimeUnit.SECONDS);
    Observable.combineLatest(source1, source2,
            (l1, l2) -> "SOURCE 1: " + l1 + " SOURCE 2: " + l2)
            .subscribe(System.out::println);
    sleep(3000);
}
 
Example 8
Project: Learning-RxJava   File: Ch10_13.java   View Source Code Vote up 5 votes
@Test
public void usingTestScheduler() {

    //Declare TestScheduler
    TestScheduler testScheduler = new TestScheduler();

    //Declare TestObserver
    TestObserver<Long> testObserver = new TestObserver<>();

    //Declare Observable emitting every 1 minute
    Observable<Long> minuteTicker =
            Observable.interval(1, TimeUnit.MINUTES,
                    testScheduler);

    //Subscribe to TestObserver
    minuteTicker.subscribe(testObserver);

    //Fast forward by 30 seconds
    testScheduler.advanceTimeBy(30, TimeUnit.SECONDS);

    //Assert no emissions have occurred yet
    testObserver.assertValueCount(0);

    //Fast forward to 70 seconds after subscription
    testScheduler.advanceTimeTo(70, TimeUnit.SECONDS);

    //Assert the first emission has occurred
    testObserver.assertValueCount(1);

    //Fast Forward to 90 minutes after subscription
    testScheduler.advanceTimeTo(90, TimeUnit.MINUTES);

    //Assert 90 emissions have occurred
    testObserver.assertValueCount(90);
}
 
Example 9
Project: Learning-RxJava   File: Ch1_3.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
        Observable<Long> secondIntervals =
                Observable.interval(1, TimeUnit.SECONDS);
        secondIntervals.subscribe(s -> System.out.println(s));
/* Hold main thread for 5 secondsso Observable above has chance to fire */
        sleep(5000);
    }
 
Example 10
Project: Learning-RxJava   File: Ch2_18.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
        Observable<Long> seconds = Observable.interval(1,
                TimeUnit.SECONDS);
//Observer 1
        seconds.subscribe(l -> System.out.println("Observer 1: " + l));
//sleep 5 seconds
        sleep(5000);
//Observer 2
        seconds.subscribe(l -> System.out.println("Observer 2: " + l));
//sleep 5 seconds
        sleep(5000);
    }
 
Example 11
Project: Learning-RxJava   File: Ch2_33.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
        Observable<Long> seconds =
                Observable.interval(1, TimeUnit.SECONDS);
        Disposable disposable =
                seconds.subscribe(l -> System.out.println("Received: " + l));
//sleep 5 seconds
        sleep(5000);
//dispose and stop emissions
        disposable.dispose();
//sleep 5 seconds to prove
//there are no more emissions
        sleep(5000);
    }
 
Example 12
Project: Learning-RxJava   File: Ch7_11.java   View Source Code Vote up 5 votes
public static void main(String[] args) {
    Observable<Long> cutOffs =
            Observable.interval(1, TimeUnit.SECONDS);
    Observable.interval(300, TimeUnit.MILLISECONDS)
            .map(i -> (i + 1) * 300) // map to elapsed time
            .window(cutOffs)
            .flatMapSingle(obs -> obs.reduce("", (total, next) ->
                    total
                            + (total.equals("") ? "" : "|") + next))
            .subscribe(System.out::println);
    sleep(5000);
}
 
Example 13
Project: dztools   File: Zorro.java   View Source Code Vote up 5 votes
public Zorro(final PluginConfig pluginConfig) {
    this.pluginConfig = pluginConfig;

    heartBeat = Observable.interval(0L,
                                    pluginConfig.zorroProgressInterval(),
                                    TimeUnit.MILLISECONDS);
}
 
Example 14
Project: Phoenix-for-VK   File: UploadQueueStore.java   View Source Code Vote up 5 votes
@SuppressLint("UseSparseArrays")
UploadQueueStore(@NonNull AppStores base) {
    super(base);
    this.statusUpdatePublishSubject = PublishSubject.create();
    this.queueUpdatesPublishSubject = PublishSubject.create();
    this.timer = Observable.interval(0L, PROGRESS_LOOKUP_DELAY, TimeUnit.MILLISECONDS);
    this.progress = Collections.synchronizedMap(new HashMap<>(0));
}
 
Example 15
Project: reactive-playing   File: TickTack.java   View Source Code Vote up 5 votes
private static Observable<Long> clock() {
    Observable<Long> fast = Observable.interval(1, TimeUnit.SECONDS);
    Observable<Long> slow = Observable.interval(3, TimeUnit.SECONDS);

    return Observable.merge(
            slow.filter(t -> isSlowTickTime()).doOnEach(w->System.out.println("<SLOW>")),
            fast.filter(t -> !isSlowTickTime()).doOnEach(w->System.out.println("<FAST>"))
    );
}
 
Example 16
Project: Reactive-Programming-With-Java-9   File: DemoCreate_Observables.java   View Source Code Vote up 5 votes
public static void demoInterval() {

		Observable<Long> myObservable = Observable.interval(2, TimeUnit.SECONDS);
		Observer<Long> myObserver = new Observer<Long>() {

			@Override
			public void onComplete() {
				// TODO Auto-generated method stub
				System.out.println("emission completed");

			}

			@Override
			public void onError(Throwable throwable) {
				// TODO Auto-generated method stub
				System.out.println("sorry you got an error");

			}

			@Override
			public void onNext(Long item) {
				// TODO Auto-generated method stub
				System.out.println("item" + item);

			}

			@Override
			public void onSubscribe(Disposable arg0) {
				// TODO Auto-generated method stub

			}
		};
		myObservable.subscribe(myObserver);
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
 
Example 17
Project: GitHub   File: IntervalExampleActivity.java   View Source Code Vote up 4 votes
private Observable<? extends Long> getObservable() {
    return Observable.interval(0, 2, TimeUnit.SECONDS);
}
 
Example 18
Project: RxJava2-Android-Sample   File: IntervalExampleActivity.java   View Source Code Vote up 4 votes
private Observable<? extends Long> getObservableWithInitDelayAndPeriod() {
    return Observable.interval(0, 2, TimeUnit.SECONDS);
}
 
Example 19
Project: RxJava2-Android-Sample   File: IntervalExampleActivity.java   View Source Code Vote up 4 votes
private Observable<? extends Long> getObservableWithPeriod() {
    return Observable.interval(1, TimeUnit.SECONDS);
}
 
Example 20
Project: rxjavatraining   File: HotVsColdObservableTest.java   View Source Code Vote up 4 votes
@Test
public void testColdObservable() throws Exception {

    Observable<Long> longObservable = Observable.interval(1,
            TimeUnit.SECONDS,
            Schedulers.from(executorService));

    longObservable.subscribe(x -> System.out.println("L1:" + x));

    Thread.sleep(2000);

    longObservable.subscribe(x -> System.out.println("L2:" + x));



    Thread.sleep(10000);
}