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 void useCompletableFuture(List<MyTask> tasks) { | |
System.out.println("Run using CompletableFutures. \nWait…"); | |
Instant startTime = Instant.now(); | |
List<CompletableFuture<Integer>> futures = tasks.stream() | |
.map(tmpTask -> { | |
return CompletableFuture.supplyAsync(() -> { | |
return tmpTask.calculate(); | |
}); |
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 void useSequential(List<MyTask> tasks) { | |
System.out.println("Run on sequential manner. \nWait…"); | |
Instant startTime = Instant.now(); | |
List<Integer> result = tasks.stream() | |
.map(MyTask::calculate) | |
.collect(Collectors.toList()); | |
Instant endTime = Instant.now(); |
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 class TestConcurrency { | |
public static final int MAX_TASKS = 10; | |
public static final int SECONDS = 1; | |
public static void main(String[] args) { | |
List<MyTask> tasks = IntStream.range(0, MAX_TASKS) | |
.mapToObj(i -> new MyTask(SECONDS)) | |
.collect(Collectors.toList()); | |
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 Concurrency { | |
public void useSequential(List<MyTask> tasks); | |
public void useCompletableFuture(List<MyTask> tasks); | |
public void useCompletableFutureWithExecutor(List<MyTask> tasks); | |
public void useParallelStream(List<MyTask> tasks); | |
} |
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
import java.util.concurrent.TimeUnit; | |
public class MyTask { | |
private final int seconds; | |
public MyTask(int seconds) { | |
this.seconds = seconds; | |
} | |
public int calculate() { | |
try { | |
TimeUnit.SECONDS.sleep(seconds); | |
} catch (final InterruptedException e) { |
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
# concurrency | |
### A quick check on performance of CompletableFuture and Parallel Stream | |
In my recent project, I had to improve the performance of existing java application. The application was written on plain java. It doesn't use any frameworks (e.g. Spring). The app was primarily written for infra purposes like handling Kubernetes workloads. Most of the time, the app is making hundreds of API calls to [Kubernetes API](https://github.com/kubernetes-client/java/) server and process the json payloads. Some of the api calls and the processing tasks can be run independently. | |
Once I got to know that I could run some tasks independently, the next thing which came to my mind was threads. As usual, I googled, and read the articles related to thread pools. Of course, I didn't miss to check [Baeldung](https://www.baeldung.com/java-concurrency) also. Then finally I decided to use `CompletableFuture` which gives you a lot of customized options to run your workloads in asynchronous way. I also wanted to give a quick c |
NewerOlder