thenRun Signature and Runnable Interface
What you'll learn: How thenRun accepts a Runnable and what that means for your asynchronous pipeline.
Understanding the Runnable Parameter
In the previous lesson, you learned that thenRun executes an action after a stage completes without accessing the result. Now let's examine how it does this through its signature.
The thenRun method accepts a Runnable — Java's classic functional interface for parameterless, void-returning actions. Here's the signature:
CompletableFuture<Void> thenRun(Runnable action)
What This Signature Tells Us
Input: A Runnable — an interface with a single method void run() that takes no parameters.
Output: A CompletableFuture<Void> — representing the completion of the action, but carrying no result value.
Since Runnable has no parameters, the action you pass to thenRun cannot access the previous stage's result. It simply executes when the previous stage finishes.
Example Usage
CompletableFuture.supplyAsync(() -> "Processing data")
.thenRun(() -> System.out.println("Task completed!"));
The lambda () -> System.out.println(...) matches the Runnable signature perfectly: no parameters, no return value.
The Runnable Connection
Think of Runnable as a "blind action" — it runs without knowing or caring what came before. This makes thenRun perfect for side-effects like logging, notifications, or cleanup operations that should happen after a task completes, regardless of its result.
Key Takeaway: thenRun takes a Runnable action and returns CompletableFuture<Void>, executing the action after the previous stage completes without accessing or returning any value.