This file contains hidden or 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
| final ExecutorService executor = Executors.newSingleThreadExecutor(); | |
| CompletableFuture.runAsync(task, executor); |
This file contains hidden or 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
| CompletableFuture<String> completableFuture = new CompletableFuture<>(); | |
| completableFuture | |
| .thenAccept(System.out::println); | |
| completableFuture.complete("I have completed!"); |
This file contains hidden or 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
| CompletableFuture<Void> chain = new CompletableFuture<>(); | |
| chain.thenComposeAsync(nil -> createUser()) | |
| .thenAcceptAsync(logNewUserId()) | |
| .thenComposeAsync(nil -> | |
| registerAddress() | |
| .thenAcceptBothAsync(registerPaymentDetails(), (address, paymentDetailsSuccess) -> { | |
| System.out.println("Registered address was : " + address); | |
| System.out.println("Registered payment details success : " + paymentDetailsSuccess); | |
| }) |
This file contains hidden or 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
| CompletableFuture<Result> chain = CompletableFuture.runAsync(() -> System.out.println("Start")) | |
| .thenCombine(subTask, (nil, text) -> { | |
| if (text == null || text.isEmpty()) | |
| throw new IllegalArgumentException("Text cannot be null or empty!"); | |
| return Result.COMPLETED; | |
| }) | |
| .exceptionally(exception -> Result.FAILED); |
This file contains hidden or 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
| CompletableFuture<String> subTask = new CompletableFuture<>(); | |
| CompletableFuture<Result> chain = CompletableFuture.runAsync(() -> System.out.println("Start")) | |
| .thenCombine(subTask, (nil, text) -> { | |
| if (text == null || text.isEmpty()) | |
| throw new IllegalArgumentException("Text cannot be null or empty!"); | |
| return Result.COMPLETED; | |
| }) | |
| .completeOnTimeout(Result.TIMEOUT, 500, TimeUnit.MILLISECONDS) | |
| .exceptionally(exception -> Result.FAILED); |
This file contains hidden or 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
| package com.theboreddev.concurrency; | |
| import com.theboreddev.Processor; | |
| import java.util.List; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.TimeUnit; | |
| import java.util.concurrent.atomic.AtomicInteger; | |
| import java.util.concurrent.atomic.LongAccumulator; |
This file contains hidden or 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
| public interface Processor { | |
| Integer process(List<Integer> input) throws InterruptedException; | |
| } |
This file contains hidden or 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
| package com.theboreddev.sequential; | |
| import com.theboreddev.Processor; | |
| import java.util.List; | |
| public class SequentialProcessor implements Processor { | |
| @Override | |
| public Integer process(List<Integer> input) { |
This file contains hidden or 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
| package com.theboreddev.parallelism; | |
| import com.theboreddev.Processor; | |
| import java.util.List; | |
| public class ParallelProcessor implements Processor { | |
| @Override | |
| public Integer process(List<Integer> input) { |
This file contains hidden or 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
| final PriceCalculatorFactory factory = new PriceCalculatorFactory(); | |
| final Item newItem = new Item(1L, new BigDecimal("12.99")); | |
| final DeliveryPriceCalculator priceCalculator = factory.priceCalculatorFor(OldStylePlan.BASIC); | |
| System.out.println("Delivery price is " + priceCalculator.priceFor(newItem)); |