Why need CountDownLatch We will demo an example to show why we need the CountDownLatch class. It is known that when we call the methods of CompletableFuture, if the method is asynchronous, then the job is executed in a new thread. The asynchronous methods will have the postfix -Async in their name. While with the…
Callback The chaining of methods in CompletableFuture is a nice way to implement callback. In functional programming, a callback is basically a function (a method in Java). It is called callback, because it will be called when a result or a job is done in the future. Say, we call the supplyAsync() method to provide…
Basic methods of CompletableFuture class part 3 We will continue with the basic methods of CompletableFuture class. With the Fruit.java in the previous section: thenCombine and thenCombineAsync We know that the thenCompose() duo can chain 2 futures, one depends on the other, for example, in our previous example, the second CompletableFuture: must depend on the…
Basic methods of CompletableFuture class part 2 We will continue with the basic methods of CompletableFuture class. The Fruit.java in the previous section: thenCompose and thenComposeAsync methods These methods return a CompletableFuture from another CompletableFuture. Curiouser and curiouser, right? Yep, this one is hard, at first only :D. Now, the methods as their name suggests,…
The CompletableFuture class The CompletableFuture class has a lot of advantages vs the Future interface. The Future interface has some shortcoming: – the get() method is blocking. – hard to combine 2 asynchronous operations, of which one is dependent on the other. – hard to callback when the result is returned (must block the main…
Introduction to Asynchronous and Synchronous Asynchronous vs synchronous programming: synchronous programing is the traditional way we write code, where Java will execute our code, instruction by instruction in sequence. So an instruction must wait for the instruction before it finishes, then it can be executed. For example: Well line B will have to wait for…
Introduction to CompletableFuture class The CompletableFuture is a class that implements the Future interface, so it has every method of Future, like get(), isDone(), isCancel() … But of course, we don’t use it that way. We will use a bean, the Fruit class for our example, in the src folder. Nothing special here, it is…
Java Test for Multithreading Don’t try to hack or to do something unusual, this is just some basic Java tests for multithreading, to see what we learned until now. Please try to finish these tests without using an IDE. After choosing your options (radio buttons) please press Check button to see the result. Note that…
Introduction to Fork/Join Framework The Fork/Join Framework was introduced from Java 7, and actually, not every Java developer seems to be happy with it. The Fork/Join will split (or fork) task into subtasks recursively, each subtask can be executed concurrently. The Fork/Join tasks and subtasks are submited to a special thread pools, the ForkJoinPool. Just…
Introduction to Fork/Join Framework The Fork/Join Framework was introduced from Java 7, and actually, not every Java developer seems to be happy with it. The Fork/Join will split (or fork) task into subtasks recursively, each subtask can be executed concurrently. The Fork/Join tasks and subtasks are submited to a special thread pools, the ForkJoinPool. Fork…