Mark As Completed Discussion

Futures and Promises

In concurrent programming, futures and promises are abstractions that represent the results of asynchronous operations.

Futures

A future is a placeholder for a value that may not be available yet. It allows you to perform other operations while waiting for the result of the asynchronous operation. Once the result becomes available, you can retrieve it from the future object.

In Java, the CompletableFuture class provides a way to create and work with futures. You can use the supplyAsync method to asynchronously execute a task and return a future representing the task's result.

Here's an example of using a future in Java with CompletableFuture:

TEXT/X-JAVA
1import java.util.concurrent.CompletableFuture;
2
3public class Main {
4  public static void main(String[] args) {
5    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
6      // Perform asynchronous task here
7      return "Hello, world!";
8    });
9
10    // Perform other tasks while waiting for the future to complete
11    System.out.println("Executing other tasks...");
12
13    // Wait for the future to complete and retrieve the result
14    String result = future.get();
15    System.out.println("Future completed: " + result);
16  }
17}

In this example, we create a CompletableFuture that executes a task asynchronously and returns the result as a future. While waiting for the future to complete, we can continue executing other tasks. Once the future is complete, we can retrieve the result using the get method.

Promises

A promise is a complement to the future. It represents a value that will be resolved in the future. Unlike a future, a promise can be completed manually by setting its value. Once the promise is completed, the associated future can retrieve the value.

Promises are commonly used in scenarios where you want to control the completion of an asynchronous operation. For example, you can create a promise and pass its associated future to another part of your code. That part can complete the promise when the asynchronous operation is done.

The CompletableFuture class in Java provides methods to create and work with promises. For example, you can use the CompletableFuture constructor to create a promise, and the complete method to manually complete the promise with a value.

Here's an example of using a promise in Java with CompletableFuture:

TEXT/X-JAVA
1import java.util.concurrent.CompletableFuture;
2
3public class Main {
4  public static void main(String[] args) {
5    CompletableFuture<String> future = new CompletableFuture<>();
6
7    // Pass the future to another part of your code
8    performAsynchronousOperation(future);
9
10    // Complete the promise with a value
11    future.complete("Hello, world!");
12
13    // Retrieve the value from the completed promise
14    String result = future.get();
15    System.out.println("Promise completed: " + result);
16  }
17
18  private static void performAsynchronousOperation(CompletableFuture<String> future) {
19    // Perform asynchronous operation and complete the promise
20    // with the result
21  }
22}
JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment