Skip to content

Instantly share code, notes, and snippets.

@jwkidd3
Created January 18, 2017 22:13
Show Gist options
  • Save jwkidd3/976f7d03fdb936208989fd74c8a81618 to your computer and use it in GitHub Desktop.
Save jwkidd3/976f7d03fdb936208989fd74c8a81618 to your computer and use it in GitHub Desktop.
final BehaviorSubject<Integer> subject = BehaviorSubject.create();
subject.subscribe(i -> {
System.out.println("Received " + i + " on " + Thread.currentThread().getId());
});
int[] i = {1}; // naughty side-effects for examples only ;)
Runnable r = () -> {
synchronized(i) {
System.out.println("onNext(" + i[0] + ") on " + Thread.currentThread().getId());
subject.onNext(i[0]++);
}
};
r.run(); // Execute on main thread
new Thread(r).start();
new Thread(r).start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment