From e15e0dcdd5539dd7d79978fc90382255570542fc Mon Sep 17 00:00:00 2001 From: SunYufei Date: Sat, 31 Aug 2024 19:26:40 +0800 Subject: [PATCH] [java] CompletableFuture --- README.md | 2 + java/.gitignore | 3 + java/pom.xml | 27 +++++++++ java/src/main/java/juc/MemberService.java | 16 ++++++ java/src/main/java/juc/OrderService.java | 16 ++++++ java/src/main/java/juc/ProductService.java | 16 ++++++ java/src/main/java/juc/ServiceRunner.java | 56 +++++++++++++++++++ java/src/test/java/juc/ServiceRunnerTest.java | 23 ++++++++ 8 files changed, 159 insertions(+) create mode 100644 java/.gitignore create mode 100644 java/pom.xml create mode 100644 java/src/main/java/juc/MemberService.java create mode 100644 java/src/main/java/juc/OrderService.java create mode 100644 java/src/main/java/juc/ProductService.java create mode 100644 java/src/main/java/juc/ServiceRunner.java create mode 100644 java/src/test/java/juc/ServiceRunnerTest.java diff --git a/README.md b/README.md index 2eef689..15698c6 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ [设计模式](design-pattern/) +[Java](java/) + [机器学习](ML/) [Spring Boot](spring-boot-demo/) diff --git a/java/.gitignore b/java/.gitignore new file mode 100644 index 0000000..3f0f465 --- /dev/null +++ b/java/.gitignore @@ -0,0 +1,3 @@ +.idea/ +target/ +.DS_Store diff --git a/java/pom.xml b/java/pom.xml new file mode 100644 index 0000000..ece99b8 --- /dev/null +++ b/java/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + ml.sun + demo + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + 5.11.0 + + + + + org.junit.jupiter + junit-jupiter + ${junit.version} + test + + + \ No newline at end of file diff --git a/java/src/main/java/juc/MemberService.java b/java/src/main/java/juc/MemberService.java new file mode 100644 index 0000000..ce0bb31 --- /dev/null +++ b/java/src/main/java/juc/MemberService.java @@ -0,0 +1,16 @@ +package juc; + +import java.util.Collections; +import java.util.Map; + +public class MemberService { + public Map search() { + System.out.println("MemberService.search();"); + try { + Thread.sleep(300); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return Collections.emptyMap(); + } +} diff --git a/java/src/main/java/juc/OrderService.java b/java/src/main/java/juc/OrderService.java new file mode 100644 index 0000000..8a086a3 --- /dev/null +++ b/java/src/main/java/juc/OrderService.java @@ -0,0 +1,16 @@ +package juc; + +import java.util.Collections; +import java.util.List; + +public class OrderService { + public List search() { + System.out.println("OrderService.search();"); + try { + Thread.sleep(400); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return Collections.emptyList(); + } +} diff --git a/java/src/main/java/juc/ProductService.java b/java/src/main/java/juc/ProductService.java new file mode 100644 index 0000000..d6efecb --- /dev/null +++ b/java/src/main/java/juc/ProductService.java @@ -0,0 +1,16 @@ +package juc; + +import java.util.Collections; +import java.util.List; + +public class ProductService { + public List search() { + System.out.println("ProductService.search();"); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return Collections.emptyList(); + } +} diff --git a/java/src/main/java/juc/ServiceRunner.java b/java/src/main/java/juc/ServiceRunner.java new file mode 100644 index 0000000..ebf4021 --- /dev/null +++ b/java/src/main/java/juc/ServiceRunner.java @@ -0,0 +1,56 @@ +package juc; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +public class ServiceRunner { + private static final MemberService MEMBER_SERVICE = new MemberService(); + private static final OrderService ORDER_SERVICE = new OrderService(); + private static final ProductService PRODUCT_SERVICE = new ProductService(); + + public static void sequence() { + MEMBER_SERVICE.search(); + ORDER_SERVICE.search(); + PRODUCT_SERVICE.search(); + } + + public static void parallel() { + CompletableFuture member = CompletableFuture.supplyAsync(MEMBER_SERVICE::search); + CompletableFuture order = CompletableFuture.supplyAsync(ORDER_SERVICE::search); + CompletableFuture product = CompletableFuture.supplyAsync(PRODUCT_SERVICE::search); + CompletableFuture.allOf(member, order, product).join(); + try { + member.get(); + order.get(); + product.get(); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + /** + * member + * / \ + * order product + */ + public static void preorder() { + CompletableFuture.completedFuture(MEMBER_SERVICE.search()).join(); + // + CompletableFuture order = CompletableFuture.supplyAsync(ORDER_SERVICE::search); + CompletableFuture product = CompletableFuture.supplyAsync(PRODUCT_SERVICE::search); + CompletableFuture.allOf(order, product).join(); + } + + /** + * member + * product - order + */ + public static void event() { + CompletableFuture member = CompletableFuture.supplyAsync(MEMBER_SERVICE::search); + CompletableFuture product = CompletableFuture.supplyAsync(PRODUCT_SERVICE::search); + CompletableFuture order = product.thenApply((products) -> ORDER_SERVICE.search()); + CompletableFuture.allOf(member, order).join(); + } +} diff --git a/java/src/test/java/juc/ServiceRunnerTest.java b/java/src/test/java/juc/ServiceRunnerTest.java new file mode 100644 index 0000000..c81a9dd --- /dev/null +++ b/java/src/test/java/juc/ServiceRunnerTest.java @@ -0,0 +1,23 @@ +package juc; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ServiceRunnerTest { + @Test + public void test() { + Assertions.assertEquals(12, run("sequence", ServiceRunner::sequence) / 100); + Assertions.assertEquals(5, run("parallel", ServiceRunner::parallel) / 100); + Assertions.assertEquals(8, run("preorder", ServiceRunner::preorder) / 100); + Assertions.assertEquals(9, run("event", ServiceRunner::event) / 100); + } + + public long run(String name, Runnable runnable) { + long startTime = System.currentTimeMillis(); + runnable.run(); + long endTime = System.currentTimeMillis(); + long duration = endTime - startTime; + System.out.println(name + " total time: " + duration + "ms\n"); + return duration; + } +}