一、截图示例

二、代码示例
2.1 接口示例
package com.learning.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* @Author wangyouhui
* @Description TODO
**/
@RestController
@RequestMapping("info")
@Slf4j
public class InfoController {
@GetMapping("/order/{id}")
public Map orderDetail(@PathVariable Long id){
log.info("开始查找订单: {}", id);
try {
Random random = new Random();
Thread.sleep(2000 + random.nextInt(2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
Map result = new HashMap<>();
result.put("id", id);
result.put("total", 1300);
log.info("查找订单结束: {}", id);
return result;
}
@GetMapping("/product/{id}")
public Map productDetail(@PathVariable Long id){
log.info("开始查找产品: {}", id);
try {
Random random = new Random();
Thread.sleep(500 + random.nextInt(2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
Map result = new HashMap<>();
if(id == 1){
result.put("id", id);
result.put("price", 300);
result.put("name", "小米耳机");
}else{
result.put("id", id);
result.put("price", 1000);
result.put("name", "三星硬盘");
}
log.info("开始查找产品: {}", id);
return result;
}
@GetMapping("/package/{id}")
public Map packageDetail(@PathVariable Long id){
log.info("开始查找快递: {}", id);
try {
Random random = new Random();
Thread.sleep(3000 + random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
Map result = new HashMap<>();
result.put("id", id);
result.put("name", "中通快递");
result.put("id", id);
log.info("开始查找快递: {}", id);
return result;
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
2.2 调用示例
package com.learning.future;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
import java.util.concurrent.*;
/**
* @Author wangyouhui
* @Description future并发执行,获取有返回值的结果
**/
@Slf4j
public class Learning_01 {
public static void main(String[] args) {
System.out.println("查询信息开始");
long begin = System.currentTimeMillis();
log.info("开始时间: {}", begin);
ExecutorService executorService = Executors.newCachedThreadPool();
RestTemplate restTemplate = new RestTemplate();
Future
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52