What is Asynchronous programming?

Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application thread and notifies the calling thread of its completion, failure or progress.

Why do we need Asynchronous programming?

The simple answer is we want to deliver a smooth user experience to our users without freezing the main thread and to make our application more responsive. To keep the main thread free we need to do a lot of heavy and time-consuming work in the background.

In asynchronous programming, thread management is the key.  While working with multiple threads, the other thread should experience minimum side effects from the other thread. That makes your code easily readable and understandable to a new person and it also makes error easily traceable. This is where reactive programming comes in.

What is Reactive Programming?

Reactive programming is asynchronous programming with observable streams. Observer pattern is used when there is one to many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically and corresponding changes are done to all dependent objects.

Think Twitter. When you say you want to follow someone, Twitter adds you to their follower list. When they sent a new tweet, you see it in your input. Same idea, in that case, your Twitter account is the Observer and the person you’re following is the Observable.

RX = OBSERVABLE + OBSERVER + SCHEDULERS

We are going to discuss these points in detail one by one.

  • Observable:  Observable are nothing but the data streams. Observable packs the data that can be passed around from one thread to another thread. There are various operators that can help observer to emit some specific data based on certain events.
  • Observers: Observers consumes the data stream emitted by the observable. Observers subscribe to the observable using subscribeOn() method to receive the data emitted by the observable. Whenever the observable emits the data all the registered observer receives the data in onNext() callback. If there is an error thrown from observable, the observer will receive it in onError().
  • Schedulers: Remember that Rx is for asynchronous programming and we need a thread management. There is where schedules come into the picture. Schedulers are the component in Rx that tells observable and observers, on which thread they should run.

Let’s look into the basic example.

Observable<String> observableString = Observable.just(new String[]{“1″,”2”,”3”); Observer<String> observer = new Observer<String>() {
@Override
public void onCompleted() {
System.out.println(“Completed!”);
}
@Override
public void onError(Throwable e) {
System.out.println(“Error!”);
}
@Override
public void onNext(String value) {
System.out.println(“onNext: ”+ value);
}
};

observableString.subscribeOn(Schedulers.newThread()) .observeOn(Schedulers.mainThread()
.subscribe(observer);

Output is:
onNext: 1
onNext: 2
onNext: 3
Complted!

Step-1 Create observable that emits the data:

Here observableString is an observable which emits the data. In our case, it emits the strings. just() is an operator.

Step -2 Create observer that consumes data:

In above code observer is an observer that consumes the data emitted by the observableString observable. It processes the data received and also handles error inside it.

Step-3 Manage concurrency:

At the last step, we define our schedulers that manage the concurrency. subscribeOn(Schedulers.newThread()) tells observableString observable to run on background thread. observeOn(Schedulers.mainThread()) tells observer to run on the main thread. This is basic code for reactive programming.

Conclusion:

To summarize, Reactive programming is the best choice to do asynchronous work with the streams of data. I hope this serves as a useful introduction to Reactive programming and an overview of its basic capabilities. It also has much more powerful concepts. To learn more about reactive extensions browse the resources available at ReactiveX.