Skip to content

Instantly share code, notes, and snippets.

@JuneBuug
Created July 16, 2020 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JuneBuug/98d0bb15c1c9b1e4b47eeac2c974c8d2 to your computer and use it in GitHub Desktop.
Save JuneBuug/98d0bb15c1c9b1e4b47eeac2c974c8d2 to your computer and use it in GitHub Desktop.
completablefuture 학습
@Slf4j
class FutureTest {
@Test
void init() {
log.info("Everything is ok");
}
@Test
void 동기_가져오기() {
Shop shop = new Shop();
log.info("모카랑 아아더하면 {}", shop.getPrice("mocha") + shop.getPrice("americano"));
}
@Test
void 비동기_가져오기() {
Shop shop = new Shop();
Integer total = shop.getPriceAsync("mocha").thenCombine(shop.getPriceAsync("americano"), Integer::sum).join();
log.info("모카랑 아아더하면 {}", total);
}
class Shop {
private Map<String, Integer> map = new HashMap<>();
public Shop() {
this.map.put("mocha", 1000);
this.map.put("latte", 700);
this.map.put("americano", 500);
}
public Integer getPrice(String name) {
try {
log.info("Thread 기다림");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return map.get(name);
}
public CompletableFuture<Integer> getPriceAsync(String name) {
return CompletableFuture.supplyAsync(() -> {
log.info("supply async");
return getPrice(name);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment