Book Image

Mastering Concurrency Programming with Java 9 - Second Edition

By : Javier Fernández González
Book Image

Mastering Concurrency Programming with Java 9 - Second Edition

By: Javier Fernández González

Overview of this book

Concurrency programming allows several large tasks to be divided into smaller sub-tasks, which are further processed as individual tasks that run in parallel. Java 9 includes a comprehensive API with lots of ready-to-use components for easily implementing powerful concurrency applications, but with high flexibility so you can adapt these components to your needs. The book starts with a full description of the design principles of concurrent applications and explains how to parallelize a sequential algorithm. You will then be introduced to Threads and Runnables, which are an integral part of Java 9's concurrency API. You will see how to use all the components of the Java concurrency API, from the basics to the most advanced techniques, and will implement them in powerful real-world concurrency applications. The book ends with a detailed description of the tools and techniques you can use to test a concurrent Java application, along with a brief insight into other concurrency mechanisms in JVM.
Table of Contents (21 chapters)
Title Page
About the Author
About the Reviewer
Customer Feedback

Introducing the Callable and Future interfaces

The Executor framework allows programmers to execute concurrent tasks without creating and managing threads. You create tasks and send them to the executor. It creates and manages the necessary threads.

In an executor, you can execute two kinds of tasks:

  • Tasks based on the Runnable interface: These tasks implement the run() method that doesn't return any results.
  • Tasks based on the Callable interface: These tasks implement the call() interface that returns an object as a result. The concrete type that will be returned by the call() method is specified by a generic type parameter of the Callable interface. To get the result returned by the task, the executor will return an implementation of the Future interface for every task.

In previous chapters, you learned how to create executors, send tasks based on the Runnable interface to it, and personalize the executor to adapt it to your needs. In this chapter, you will learn how to work with tasks based...