JavaData & JavaBest Practices of Asynchronous Programming With Java

Best Practices of Asynchronous Programming With Java

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Asynchronous programming is very popular these days, primarily because of its ability to improve the overall throughput on a multi-core system. Asynchronous programming is a programming paradigm that facilitates fast and responsive user interfaces. The asynchronous programming model in Java provides a consistent programming model to write programs that support asynchrony. This article discusses the best practices that should be followed while writing code to perform asynchronous operations in Java.

What Is Asynchrony and Why Is It Needed?

Asynchronous programming provides a non-blocking, event-driven programming model. This programming model leverages the multiple cores in your system to provide parallelization by using multiple CPU cores to execute the tasks, thus increasing the application’s throughput. Note that throughput is a measure of the amount of work done in unit time. In this programming paradigm, a unit of work would execute separately from the main application thread and notify the calling thread about its execution state: success, in progress or failure.

We need asynchrony to eliminate the blocking model. In essence, the asynchronous programming model can use the same thread to process multiple requests without any request blocking the thread. Imagine a threading model in which an application executes a task and then waits for the task to complete before proceeding. A good example of this can be logging frameworks: You typically would want to log exceptions and errors into your log targets; in other words, file, database, or something similar. There is no point for your application to wait till the logging tasks are over. In doing so, the application’s responsiveness would be affected. On the contrary, if the call to the logging framework can be made asynchronously, the application can proceed with other tasks concurrently, without having to wait. This is an example of a non-blocking mode of execution.

To implement asynchrony in Java, you would need to use Future or FutureTask, available in the java.util.concurrent package. Although the former is an interface, the latter is an implementation of the Future interface. In essence, in using “Future” in your code, your asynchronous task will be executed immediately with the promise of the result being made available to the calling thread in the future.

The following code snippet shows an interface with two methods. One illustrates a synchronous method and the other an asynchronous method.

import java.util.concurrent.Future;
public interface IDataManager {
   // synchronous method
   public String getDataSynchronously();
   // asynchronous method
   public Future<String> getDataAsynchronously();
}

It should be noted that one of the major drawbacks of the callback model is that it can grow sideways when the callbacks are nested together.

What You Should and Shouldn’t Do

To facilitate testing, you should separate or isolate functionality from multithreading in your code. When writing asynchronous code in Java, you should always follow the asynchronous callback model so that the calling thread is not blocked.

Note that constructors cannot be asynchronous and you should not call asynchronous methods inside a constructor. Asynchrony is particularly useful when the tasks executed asynchronously are not dependent on one another. You should not use asynchrony when the invoking task depends on the called task (asynchronous method) for it to proceed.

You should handle exceptions inside the asynchronous methods (if they are prone to raise or throw exceptions). You should not implement asynchronous methods for long, time-consuming tasks. A long running task, if executed asynchronously, might take a longer time than the same task executed synchronously because the runtime would perform context switches, thread state storages, and so forth for methods that execute asynchronously. You also should note the difference between synchronous and asynchronous exceptions. the former implies exceptions that would occur at a particular program statement every time the program is executed; asynchronous exceptions are those that are much more difficult to handle and can occur anywhere in your code. Here, synchronous and asynchronous exceptions imply synchronous or asynchronous code in your program that might raise exceptions.

Suggested Readings

Summary

Asynchrony is critical for developing applications that are scalable and responsive. The asynchronous callback programming model has many benefits; you can depend on asynchronous callback methods to perform expensive I/O while your processor is busy executing some other task. However, asynchronous programming is not that easy as it seems with the support provided in programming languages like C# and Java. Asynchrony should be used judiciously: You should be able to analyze when to and when not to call methods asynchronously.

In this article, we explored the concept of asynchronous programming and how it can be implemented by using the Java programming language. The article also presented the best practices in using asynchronous programming in Java. Happy reading!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories