Skip to content

Commit

Permalink
[java] CompletableFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
SunYufei committed Aug 31, 2024
1 parent 2eeed7b commit e15e0dc
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

[设计模式](design-pattern/)

[Java](java/)

[机器学习](ML/)

[Spring Boot](spring-boot-demo/)
Expand Down
3 changes: 3 additions & 0 deletions java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
target/
.DS_Store
27 changes: 27 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ml.sun</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- -->
<junit.version>5.11.0</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
16 changes: 16 additions & 0 deletions java/src/main/java/juc/MemberService.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
16 changes: 16 additions & 0 deletions java/src/main/java/juc/OrderService.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
16 changes: 16 additions & 0 deletions java/src/main/java/juc/ProductService.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
56 changes: 56 additions & 0 deletions java/src/main/java/juc/ServiceRunner.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
23 changes: 23 additions & 0 deletions java/src/test/java/juc/ServiceRunnerTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit e15e0dc

Please sign in to comment.