Created
July 16, 2020 09:29
-
-
Save JuneBuug/98d0bb15c1c9b1e4b47eeac2c974c8d2 to your computer and use it in GitHub Desktop.
completablefuture 학습
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
@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