How Java Is Used for Asynchronous Non-blocking Programming

By Chending

1) Synchronous HTTP Call

Reverse geographic interface: You can obtain the county, district, city, and province as well as the response code that the longitude and latitude represent through the longitude and latitude:


Execution at the server with the simplest synchronous call method:

Before the server responds, the Input/Output (IO) is blocked on the native method of

Through the jstack log, you can find that the Thread remains in the runnable state:

"main"#1 prio=5 os_prio=31 tid=0x00007fed0c810000 nid=0x1003 runnable [0x000070000ce14000]   java.lang.Thread.State: RUNNABLE
at Method)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(
at org.apache.http.protocol.HttpRequestExecutor.execute(
at org.apache.http.impl.execchain.MainClientExec.execute(
at org.apache.http.impl.execchain.ProtocolExec.execute(
at org.apache.http.impl.execchain.RetryExec.execute(
at org.apache.http.impl.execchain.RedirectExec.execute(
at org.apache.http.impl.client.InternalHttpClient.doExecute(
at org.apache.http.impl.client.CloseableHttpClient.execute(
at org.apache.http.impl.client.CloseableHttpClient.execute(
at com.amap.aos.async.AsyncIO.blockingIO(

Thread model:

The biggest problem of synchronization is that thread resources are not utilized fully when waiting for IO responses, resulting in limited business throughput in a large number of IO scenarios.

2) JDK NIO and Future

Generally, not all Futures are implemented in this way. For example, io.netty.util.concurrent. AbstractFuture is implemented through thread polling.

An advantage of this implementation is that the main thread can do other tasks without waiting for IO responses, such as sending another IO request which can be returned together with the IO responses:

"main"#1 prio=5 os_prio=31 tid=0x00007fd7a500b000 nid=0xe03 waiting on condition [0x000070000a95d000]   java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x000000076ee2d768> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.park(
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(
at java.util.concurrent.CountDownLatch.await(
at org.asynchttpclient.netty.NettyResponseFuture.get(
at com.amap.aos.async.AsyncIO.futureBlockingGet(
"AsyncHttpClient-2-1"#11 prio=5 os_prio=31 tid=0x00007fd7a7247800 nid=0x340b runnable [0x000070000ba94000] java.lang.Thread.State: RUNNABLE
at Method)
- locked <0x000000076eb00ef0> (a
- locked <0x000000076eb00f10> (a java.util.Collections$UnmodifiableSet)
- locked <0x000000076eb00ea0> (a
at io.netty.util.concurrent.SingleThreadEventExecutor$
at io.netty.util.concurrent.DefaultThreadFactory$

However, the main thread still needs to wait until the results are returned. This problem is not fundamentally resolved.

3) Use the Callback Method

As shown in the above figure, the main thread no longer needs to pay attention to the business logic after initiating IO. After sending the request, the main thread can complete other tasks or return to the thread pool for scheduling. If it is HttpServer, it is necessary to combine the asynchronous Servlet of Servlet 3.1.

For more information about asynchronous Servlet, refer

With the Callback method, you can see in the thread model that the thread resources have been utilized fully, and there is no thread blocking during the whole process.

4) Callback Hell

Take a typical scenario as an example. The reverse geographical interface of the administrative region — adcode — is obtained through the longitude and latitude. Then, you can obtain the local weather information through the adcode.

Such problems basically don’t arise in the synchronous programming model.

Fig: Core defects of the Callback method

5) JDK 1.8 — CompletableFuture

The reverse geographical Callback logic is encapsulated into an independent CompletableFuture. When the asynchronous thread is called back, future.complete(T) is called to encapsulate the result.

The Call logic executed by weather is encapsulated into an independent CompletableFuture. After the procedure, the logic is the same as above.

Compose indicates join and whenComplete displays output:

You can encapsulate each IO operation as an independent CompletableFuture to avoid the Callback hell problem.

CompletableFuture has only two attributes:

  • result: The result of Future execution (either the result or boxed AltResult)
  • stack: The operation stack that defines the next operation action of the Future (top of Treiber stack of dependent actions)

How is the weatherFuture method called?

The stack shows that the obtained adcode is used as a parameter to execute the following logic when reverseCOdeFuture.complete(result) is executed.

This solution solves the Callback hell problem perfectly. It looks like the synchronous coding in the main logic.

6) Vert.x Future

The core execution logic is similar:

Of course, this is not what the Vert.x is all about. However, we don’t provide more details here.

7) Reactive Streams

Core abstraction: In the entire package, there are only four interfaces — Publisher, Subscriber, Processor, and Subscription — without one for implementation.

In JDK 9, they have been encapsulated as a specification in java.util.concurrent.Flow:

The following is a simple example:

8) Reactor, Spring 5, and Spring WebFlux


Original Source:

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.