Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 指定秒数かかるAPI
*
* @param seconds
* @return
* @throws InterruptedException
*/
@RequestMapping(path = "/sleep/{seconds}", method = RequestMethod.GET)
public ResponseEntity<String> sleep(@PathVariable String seconds) throws InterruptedException {
Thread.sleep(Long.valueOf(seconds) * 1000L); // スリープするだけ
return ResponseEntity.ok("test");
}
/**
* parallelで並列実行
*
* @param seconds
* @return
*/
@RequestMapping(path = "/test/{seconds}", method = RequestMethod.GET)
public ResponseEntity<String> test(@PathVariable String seconds) {
long startTime = System.currentTimeMillis();
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
RestTemplate restTemplate = restTemplateBuilder.build();
String url = String.format("http://localhost:8080/sleep/%s", seconds);
List<String> list = List.of(url, url); // 並列
String result =
list.stream()
.parallel()
.map(
e -> {
return restTemplate.getForObject(url, String.class);
})
.collect(Collectors.joining(",", "{", "}"));
long endTime = System.currentTimeMillis();
System.out.println("処理時間:" + (endTime - startTime) + " ms");
return ResponseEntity.ok(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment