-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
|
||
[设计模式](design-pattern/) | ||
|
||
[Java](java/) | ||
|
||
[机器学习](ML/) | ||
|
||
[Spring Boot](spring-boot-demo/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
target/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |