Java Reference
In-Depth Information
Asynchronous programming radically modii es the execution order and therefore debugging.
Because debugging relies on suspending the execution and then stepping line by line through it,
it is more difi cult to understand the execution behavior and to mimic what is really happening.
The JVM determines the execution order of threads at run time. It is almost impossible to
simulate the same behavior because of very different available resources on test and development
environments. If you don't need it, asynchronous execution adds undesired complexity.
Threading and asynchronous execution can be a great tool only if used properly without
starving resources. It is a good idea to run nonblocking parts asynchronously, but not on
every method.
SUMMARY
In the age of multicores and web 2.0, asynchronous programming uses computing resources,
delegates nonblocking tasks, and results in faster and more responsive user interfaces. Even if your
application does not implement the asynchronous pattern, most application servers and JVMs use
asynchronous execution internally via thread pools for many operations. Using those available
threads and resources greatly affects your application's efi ciency and responsiveness.
Threading has been a i rst‐class citizen from the early days of Java, but using threads to run
asynchronous tasks was complicated and wasn't always safe in server‐managed containers. With the
release of the Concurrency Framework, Java unleashed a huge set of tools into the hands of Java
developers.
Java EE followed this trend by providing an easy‐to‐use and implement annotation‐based
asynchronous programming model. Adding the @Asynchronous annotation tells the container to
execute the function asynchronously.
The servlet API introduced important changes in release 3.0 and further improvements in release
3.1. The new servlet API uses the new nonblocking Java I/O to support asynchronous web
programming in an efi cient way. Although previous approaches needed a request/response couple to
be bound to a thread, the new model can use or release threads using the internal thread pool that
the container provides.
Today Java EE offers all the needed tools to run asynchronous code without the need for a third‐
party framework such as Spring or Quartz. This makes the asynchronous pattern a great tool to
implement if you want to execute nonblocking code asynchronously with almost no boilerplate code.
EXERCISES
1. Create an implementation of a session bean that has an asynchronous method.
2. Develop simple functionality that uses asynchronous methodology to persist application log
data to a database.
3. Use the asynchronous feature of Servlet 3.0 to design an asynchronous web service.
 
Search WWH ::




Custom Search