ReactiveX Asynchronous Programming In Java

ReactiveX Asynchronous Programming In Java

There is a much detailed blog about ReactiveX of myself, checkout here.

Table of Contents

  • What is Asynchronous programming?
  • Ways of Asynchronous programming in java
  • Java Callback functions
  • Java Future
  • Benefit of RxJava


1 What is Asynchronous programming?

The asynchronous issue exists, because of the blocking nature of every activity in reality. Every operation consume time, in theory, computers need to take at least one frequency to execute one instruct, it takes time between every oral conversation in the speed of voice at least. That's the blocking nature of this world, and the most valuable resource of all is the time. Some times the computer cannot wait for that longer for the blocking API, like in network communication, it waste of the computer resource so much.

The another reason is the event-based scenario. In a network connected chatting app, program listens for every coming data continuously and output the parsed result that in the screen.

Unfortunately, most of the languages we met are not asynchronously designed, there is one exception as far as I know, the famous nodejs. So the Asynchronous programming is to take the challenge and to solve that problem.

2 Ways of Asynchronous programming in java

So, How to design asynchronous API in this blocking world? before that let's see How to design synchronized API.

class SynApi {
    public Object run();
}

that's pretty native, is it? without any third dependency.

Next the unblocking asynchronous ones, as far as I known, I used three ways to implement that.
  1. Use callback and listener, in design pattern's language, is an Observable pattern.
  2. Use Future in Java Language.
  3. Use ReactiveX, Rx.java for Java

3 Java Callback functions

interface AsynApi {
    public void run(ApiCallback callback);

    public void registerListener(ApiObjListener listener);
    public void unRegisterListener(ApiObjListener listener);
}

interface ApiCallback {
    public void onRecivedData(Object obj);
}

interface ApiObjListener {
    public void onRecivedData(Object obj);
}

The difference between callback and listener, I believe they are mostly a coding style issue. The Callback used as a normal blocking API, while Listener used for listening streaming and continuous data flow.

4 Java Future

Before introduce it, we review how to do concurrency programming in java, for example running a time-consuming task in the background, the most familiar way is to create a thread pool and commit the runnable object into it.

ExecutorService executor = Executors.newFixedThreadPool(5);
executor.execute(runnable);

what if we wanna return values for every runnable? That's how java Future works.

ExecutorService executor = Executors.newFixedThreadPool(5);
executor.executor(callable);
callable.get() // blocking the threader and get the result.

5 Benefit of RxJava

Then, the RxJava came up, which is just one implementation of ReactiveX protocol. ReactiveX is invented and promoted by NetFlix, to solve the asynchronous problems.

After Rx(ReactiveX), only patterns left in asynchronous filed, Observable, Observer, Subscriber. what's more, We can schedule the working threaders both in observer and subscriber environment. Asynchronous programming became simpler, understandable, and much fun if we leverage this NetFlix technology.

I will write another isolated article to guide the Rx programming, as soon as possible. I love Rx.

Ok, demo show, https://github.com/suzp1984/RxJavaDemo. This demo illustrates how to design an asynchronous api by callbacks, Future/Callable, and Rx.

Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp