GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java Future - using Future in asynchronous Java programming Java Futurelast modified January 27, 2024 Java Future tutorial shows how to do asynchronous programming in Java using Future.
The value is retrieved from a future with
Futures have several shortcomings. For instance, they cannot be manually completed
and they do not notify when they are completed. Futures cannot be chained
and combined. In addition, there is no exception handling.
To address this shortcomings, Java 8 introduced Java Future exampleThe following example uses futures to compute factorials. com/zetcode/FactorialCalculator.java
package com.zetcode;
import java.math.BigInteger;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
public class FactorialCalculator implements Callable<BigInteger> {
private int value;
public FactorialCalculator(int value) {
this.value = value;
}
@Override
public BigInteger call() throws Exception {
var result = BigInteger.valueOf(1);
if (value == 0 || value == 1) {
result = BigInteger.valueOf(1);
} else {
for (int i = 2; i <= value; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
}
TimeUnit.MILLISECONDS.sleep(500);
return result;
}
}
The public class FactorialCalculator implements Callable<BigInteger> {
The TimeUnit.MILLISECONDS.sleep(1500); We slow down the computation a bit. com/zetcode/JavaFutureEx.java
package com.zetcode;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class JavaFutureEx {
public static void main(String[] args) throws ExecutionException, InterruptedException {
var executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
List<Map<Integer, Future<BigInteger>>> resultList = new ArrayList<>();
var random = new Random();
for (int i = 0; i < 6; i++) {
int number = random.nextInt(100) + 10;
var factorialCalculator = new FactorialCalculator(number);
Map<Integer, Future<BigInteger>> result = new HashMap<>();
result.put(number, executor.submit(factorialCalculator));
resultList.add(result);
}
for (Map<Integer, Future<BigInteger>> pair : resultList) {
var optional = pair.keySet().stream().findFirst();
if (!optional.isPresent()) {
return;
}
var key = optional.get();
System.out.printf("Value is: %d%n", key);
var future = pair.get(key);
var result = future.get();
var isDone = future.isDone();
System.out.printf("Result is %d%n", result);
System.out.printf("Task done: %b%n", isDone);
System.out.println("--------------------");
}
executor.shutdown();
}
}
We generate six random integers and calculate their factorial. var executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
The executor service handles the lifecycle of asynchronous tasks. Its var factorialCalculator = new FactorialCalculator(number);
A Map<Integer, Future<BigInteger>> result = new HashMap<>(); result.put(number, executor.submit(factorialCalculator)); resultList.add(result); We submit the tasks to the executor. We put the integer value and the future in a map so that we have the value and the computed factorial at hand. for (Map<Integer, Future<BigInteger>> pair : resultList) {
We go through the list of results. Note that the futures are returned quickly before their values are computed. var optional = pair.keySet().stream().findFirst();
if (!optional.isPresent()) {
return;
}
var key = optional.get();
We get the key of the pair. var future = pair.get(key); var result = future.get();
Using the key we get the future. When we call Value is: 39 Result is 20397882081197443358640281739902897356800000000 Task done: true -------------------- Value is: 99 Result is 933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000 Task done: true -------------------- Value is: 39 Result is 20397882081197443358640281739902897356800000000 Task done: true -------------------- Value is: 102 Result is 961446671503512660926865558697259548455355905059659464369444714048531715130254590603314961882364451384985595980362059157503710042865532928000000000000000000000000 Task done: true -------------------- Value is: 12 Result is 479001600 Task done: true -------------------- Value is: 49 Result is 608281864034267560872252163321295376887552831379210240000000000 Task done: true -------------------- SourceJava Future - language reference In this article we have used Java's Future. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |