java.util.concurrent.CompletableFuture
is a versatile utility in Java (since Java 8).It provides a means of task execution, both synchronous and asynchronous.
A CompletableFuture implements java.util.concurrent.Future
(available since Java 5) and java.util.concurrent.CompletionStage
(available since Java 8) and adds additional methods for operating on tasks.
Some additional documentation about the three in this kata:
A special lookup for exception handling:
A code-kata is a coding exercise that builds muscle memory by a practice of programming to arrive at a known solution.
The essence of the exercise (presentation material and code kata) is to demonstrate the usage patterns for the java API or functionality.
This set of code katas rely on fixing broken tests. The tests may have multiple solutions, the intent is to learn and experiment.
The project contains several JUnit tests that fail.
-
Run the test class(es).
-
One or more tests will fail with the test failure message.
-
Fix the failing tests by using
HINT
andTODO
comments. -
Repeat above steps until all tests pass.
-
Check the solutions to see if there are other ways to solve. (Remember, the solution may be less performant/optimal than yours)
-
Rinse and repeat (delete and checkout again, then back to Step 1) to build muscle memory.
How to prepare for coding along
This kata is developed as a Java maven project. Ensure that you have:
-
Apache Maven 3.6.x or above. Tested with Apache Maven 3.6.3. Link: https://maven.apache.org/download.cgi
-
JDK 11 or above. Tested with OpenJDK 16 Link: http://jdk.java.net/11/
-
Your favorite Java IDE. IntelliJ IDEA Ultimate was used to develop this kata.
The structure of the project:
|____pom.xml
|____README.adoc <------------------- This file
|____assets
| |____docs <------------------- Documentation
| |____images
|____src
| |____test <------------------- Kata Tests
| | |____java
| | |____none
| | |____cvg
| | |____futures
| |____solutions <------------------- Solutions
| | |____java
| | |____none
| | |____cvg
| | |____futures
The JUnit tests listed below are set up to utilize the Java CompletableFuture API features.
- TestKata1SimpleCompletableFutureOperations.java
-
covers the basic features of creating CompletableFuture instances and fetching results.
- TestKata2HandleExceptionsInCompletableFuture.java
-
covers how CompletableFuture exceptions are handled.
Also check: Tabulated Exception Handling in CompletableFuture - TestKata3CompletableFutureExecutions.java
-
covers some common executions in CompletableFuture.
- TestKata4CompletableFutureChaining.java
-
covers CompletableFuture chaining.
- TestKata5CompletableFutureCombinations.java
-
Tcovers a few common combinations in CompletableFuture.
Kata Test | Solution |
---|---|