Browse Source

spring-boot-demo-async 完成

pull/1/head
Yangkai.Shen 6 years ago
parent
commit
0e84a22a06
5 changed files with 179 additions and 0 deletions
  1. +6
    -0
      spring-boot-demo-async/pom.xml
  2. +15
    -0
      spring-boot-demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java
  3. +81
    -0
      spring-boot-demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java
  4. +16
    -0
      spring-boot-demo-async/src/main/resources/application.yml
  5. +61
    -0
      spring-boot-demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java

+ 6
- 0
spring-boot-demo-async/pom.xml View File

@@ -33,6 +33,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>


+ 15
- 0
spring-boot-demo-async/src/main/java/com/xkcoding/async/SpringBootDemoAsyncApplication.java View File

@@ -2,7 +2,22 @@ package com.xkcoding.async;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

/**
* <p>
* 启动器
* </p>
*
* @package: com.xkcoding.async
* @description: 启动器
* @author: yangkai.shen
* @date: Created in 2018-12-29 10:28
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@EnableAsync
@SpringBootApplication
public class SpringBootDemoAsyncApplication {



+ 81
- 0
spring-boot-demo-async/src/main/java/com/xkcoding/async/task/TaskFactory.java View File

@@ -0,0 +1,81 @@
package com.xkcoding.async.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
* <p>
* 任务工厂
* </p>
*
* @package: com.xkcoding.async.task
* @description: 任务工厂
* @author: yangkai.shen
* @date: Created in 2018-12-29 10:37
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@Component
@Slf4j
public class TaskFactory {

/**
* 模拟5秒的异步任务
*/
@Async
public Future<Boolean> asyncTask1() throws InterruptedException {
doTask("asyncTask1", 5);
return new AsyncResult<>(Boolean.TRUE);
}

/**
* 模拟2秒的异步任务
*/
@Async
public Future<Boolean> asyncTask2() throws InterruptedException {
doTask("asyncTask2", 2);
return new AsyncResult<>(Boolean.TRUE);
}

/**
* 模拟3秒的异步任务
*/
@Async
public Future<Boolean> asyncTask3() throws InterruptedException {
doTask("asyncTask3", 3);
return new AsyncResult<>(Boolean.TRUE);
}

/**
* 模拟5秒的同步任务
*/
public void task1() throws InterruptedException {
doTask("task1", 5);
}

/**
* 模拟2秒的同步任务
*/
public void task2() throws InterruptedException {
doTask("task2", 2);
}

/**
* 模拟3秒的同步任务
*/
public void task3() throws InterruptedException {
doTask("task3", 3);
}

private void doTask(String taskName, Integer time) throws InterruptedException {
log.info("{}开始执行,当前线程名称【{}】", taskName, Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(time);
log.info("{}执行成功,当前线程名称【{}】", taskName, Thread.currentThread().getName());
}
}

+ 16
- 0
spring-boot-demo-async/src/main/resources/application.yml View File

@@ -0,0 +1,16 @@
spring:
task:
execution:
pool:
# 最大线程数
max-size: 16
# 核心线程数
core-size: 16
# 存活时间
keep-alive: 10s
# 队列大小
queue-capacity: 100
# 是否允许核心线程超时
allow-core-thread-timeout: true
# 线程名称前缀
thread-name-prefix: async-task-

+ 61
- 0
spring-boot-demo-async/src/test/java/com/xkcoding/async/task/TaskFactoryTest.java View File

@@ -0,0 +1,61 @@
package com.xkcoding.async.task;

import com.xkcoding.async.SpringBootDemoAsyncApplicationTests;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
* <p>
* 测试任务
* </p>
*
* @package: com.xkcoding.async.task
* @description: 测试任务
* @author: yangkai.shen
* @date: Created in 2018-12-29 10:49
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@Slf4j
public class TaskFactoryTest extends SpringBootDemoAsyncApplicationTests {
@Autowired
private TaskFactory task;

/**
* 测试异步任务
*/
@Test
public void asyncTaskTest() throws InterruptedException, ExecutionException {
long start = System.currentTimeMillis();
Future<Boolean> asyncTask1 = task.asyncTask1();
Future<Boolean> asyncTask2 = task.asyncTask2();
Future<Boolean> asyncTask3 = task.asyncTask3();

// 调用 get() 阻塞主线程
asyncTask1.get();
asyncTask2.get();
asyncTask3.get();
long end = System.currentTimeMillis();

log.info("异步任务全部执行结束,总耗时:{} 毫秒", (end - start));
}

/**
* 测试同步任务
*/
@Test
public void taskTest() throws InterruptedException {
long start = System.currentTimeMillis();
task.task1();
task.task2();
task.task3();
long end = System.currentTimeMillis();

log.info("同步任务全部执行结束,总耗时:{} 毫秒", (end - start));
}
}

Loading…
Cancel
Save