forked from dyc87112/SpringBoot-Learning
-
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
6 changed files
with
145 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.5.1</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<groupId>com.didispace</groupId> | ||
<artifactId>chapter7-6</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<description>@Async异步任务的线程池配置</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
46 changes: 46 additions & 0 deletions
46
2.x/chapter7-6/src/main/java/com/didispace/chapter76/AsyncTasks.java
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,46 @@ | ||
package com.didispace.chapter76; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Random; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Slf4j | ||
@Component | ||
public class AsyncTasks { | ||
|
||
public static Random random = new Random(); | ||
|
||
@Async | ||
public CompletableFuture<String> doTaskOne() throws Exception { | ||
log.info("开始做任务一"); | ||
long start = System.currentTimeMillis(); | ||
Thread.sleep(random.nextInt(10000)); | ||
long end = System.currentTimeMillis(); | ||
log.info("完成任务一,耗时:" + (end - start) + "毫秒"); | ||
return CompletableFuture.completedFuture("任务一完成"); | ||
} | ||
|
||
@Async | ||
public CompletableFuture<String> doTaskTwo() throws Exception { | ||
log.info("开始做任务二"); | ||
long start = System.currentTimeMillis(); | ||
Thread.sleep(random.nextInt(10000)); | ||
long end = System.currentTimeMillis(); | ||
log.info("完成任务二,耗时:" + (end - start) + "毫秒"); | ||
return CompletableFuture.completedFuture("任务二完成"); | ||
} | ||
|
||
@Async | ||
public CompletableFuture<String> doTaskThree() throws Exception { | ||
log.info("开始做任务三"); | ||
long start = System.currentTimeMillis(); | ||
Thread.sleep(random.nextInt(10000)); | ||
long end = System.currentTimeMillis(); | ||
log.info("完成任务三,耗时:" + (end - start) + "毫秒"); | ||
return CompletableFuture.completedFuture("任务三完成"); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
2.x/chapter7-6/src/main/java/com/didispace/chapter76/Chapter76Application.java
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,15 @@ | ||
package com.didispace.chapter76; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
|
||
@EnableAsync | ||
@SpringBootApplication | ||
public class Chapter76Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Chapter76Application.class, args); | ||
} | ||
|
||
} |
Empty file.
33 changes: 33 additions & 0 deletions
33
2.x/chapter7-6/src/test/java/com/didispace/chapter76/Chapter76ApplicationTests.java
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,33 @@ | ||
package com.didispace.chapter76; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
|
||
@Slf4j | ||
@SpringBootTest | ||
public class Chapter76ApplicationTests { | ||
|
||
@Autowired | ||
private AsyncTasks asyncTasks; | ||
|
||
@Test | ||
public void test() throws Exception { | ||
long start = System.currentTimeMillis(); | ||
|
||
CompletableFuture<String> task1 = asyncTasks.doTaskOne(); | ||
CompletableFuture<String> task2 = asyncTasks.doTaskTwo(); | ||
CompletableFuture<String> task3 = asyncTasks.doTaskThree(); | ||
|
||
CompletableFuture.allOf(task1, task2, task3).join(); | ||
|
||
long end = System.currentTimeMillis(); | ||
|
||
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒"); | ||
} | ||
|
||
} |
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