Prove sulla concorrenza con ComposableFuture
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package org.gym.fp.moderjava;
|
package org.gym.fp.moderjava;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import org.gym.fp.moderjava.concurrent.Shop;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.Arrays;
|
||||||
import java.util.concurrent.Future;
|
import java.util.Collection;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
import static java.lang.System.out;
|
import static java.lang.System.out;
|
||||||
|
|
||||||
@@ -11,9 +12,10 @@ public class ConcurrencyTest {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
doExecutorsServiceTest();
|
doExecutorsServiceTest();
|
||||||
|
doFutureTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void doExecutorsServiceTest() throws ExecutionException, InterruptedException {
|
private static void doExecutorsServiceTest() throws Exception {
|
||||||
int x = 1337;
|
int x = 1337;
|
||||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||||
Future<Integer> y = executorService.submit(() -> f(x));
|
Future<Integer> y = executorService.submit(() -> f(x));
|
||||||
@@ -22,6 +24,18 @@ public class ConcurrencyTest {
|
|||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void doFutureTest() throws Exception {
|
||||||
|
Collection<Shop> shops = getShops();
|
||||||
|
Future<Integer> future = CompletableFuture.supplyAsync(() -> {
|
||||||
|
out.println("Primo completable future");
|
||||||
|
return 10;
|
||||||
|
}).thenCombine(CompletableFuture.supplyAsync(() -> {
|
||||||
|
out.println("Secondo completable future");
|
||||||
|
return 30;
|
||||||
|
}), (first, second) -> first + second);
|
||||||
|
out.println(future.get(1, TimeUnit.NANOSECONDS));
|
||||||
|
}
|
||||||
|
|
||||||
static int f(int x) {
|
static int f(int x) {
|
||||||
return 42;
|
return 42;
|
||||||
}
|
}
|
||||||
@@ -30,4 +44,17 @@ public class ConcurrencyTest {
|
|||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Collection<Shop> getShops() {
|
||||||
|
return Arrays.asList(
|
||||||
|
new Shop("BestShop1"),
|
||||||
|
new Shop("BestShop2"),
|
||||||
|
new Shop("BestShop3"),
|
||||||
|
new Shop("BestShop4"),
|
||||||
|
new Shop("BestShop5"),
|
||||||
|
new Shop("BestShop6"),
|
||||||
|
new Shop("BestShop7"),
|
||||||
|
new Shop("BestShop8")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.gym.fp.moderjava.concurrent;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Shop {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public double getPrice(String product) {
|
||||||
|
return calculatePrice(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Future<Double> getPriceAsync(String product) {
|
||||||
|
return CompletableFuture.supplyAsync(() -> calculatePrice(product));
|
||||||
|
}
|
||||||
|
|
||||||
|
private double calculatePrice(String product) {
|
||||||
|
delay();
|
||||||
|
return new Random().nextDouble() * product.charAt(0) + product.charAt(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void delay() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000L);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user