Created
October 16, 2014 12:47
-
-
Save johnoliver/a3901063f9d73d5f5c4b to your computer and use it in GitHub Desktop.
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 rx.Observable; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class TestRxJava { | |
public static void main(String[] args) { | |
Observable<Integer> obs1 = Observable.<Integer>create(aSubscriber -> { | |
AtomicInteger i = new AtomicInteger(0); | |
Executors.newScheduledThreadPool(10).scheduleAtFixedRate(() -> { | |
aSubscriber.onNext(i.get()); | |
if (i.incrementAndGet() == 10) { | |
System.out.println("One Finished sending"); | |
aSubscriber.onCompleted(); | |
} | |
}, 500, 500, TimeUnit.MILLISECONDS); | |
System.out.println("One subscribed to"); | |
return null; | |
}); | |
Observable<Integer> obs2 = Observable.<Integer>create(aSubscriber -> { | |
System.out.println("Two subscribed to"); | |
for (int i = 1; i < 10; i++) { | |
aSubscriber.onNext(i); | |
} | |
System.out.println("Two Finished sending"); | |
aSubscriber.onCompleted(); | |
System.out.println("Two Finished complete"); | |
return null; | |
}); | |
Observable.concat(obs1, obs2).subscribe(num -> { | |
System.out.println("Received: " + num); | |
}); | |
System.out.println("Main done"); | |
} | |
} | |
``` | |
``` | |
One subscribed to | |
Main done | |
Received: 0 | |
Received: 1 | |
Received: 2 | |
Received: 3 | |
Received: 4 | |
Received: 5 | |
Received: 6 | |
Received: 7 | |
Received: 8 | |
Received: 9 | |
One Finished sending | |
Two subscribed to | |
Received: 1 | |
Received: 2 | |
Received: 3 | |
Received: 4 | |
Received: 5 | |
Received: 6 | |
Received: 7 | |
Received: 8 | |
Received: 9 | |
Two Finished sending | |
Two Finished complete | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment