1596808380
With the introduction of CompletableFuture
in Java 8, the world of asynchronouse programming took a massive step forward. But one may wonder the real reason behind its introduction when we already had **Future **interfacesince Java 5. The real motivation behind CompletableFuture was to overcome certain limitations of **Future **interface:
1\. There was no way to make get() call non-blocking in Future.
2\. No support for exception handling.
3\. Chaining of callbacks was not supported.
4\. There was no way to explicitly complete the task.
In this article, we will see some common usage of CompletableFuture
.
First of all to create a new completableFuture
task, simply call the constructor
CompletableFuture<String> task = new CompletableFuture<String>();
Also, you can explicitly create a completed task
CompletableFuture<String> completedTask =
CompletableFuture.completedFuture("Some Value");
Or mark the task as completed by calling the complete
method. All the clients waiting for task to complete will get the specified result.
task.complete("Task completed forcefully");
To get the result of this CompletableFuture
we can call get()
method.
if(completedTask.isDone()){
completedTask.get();
}
#java #completablefuture #parallel-computing #tutorial #asynchronous-programming
1596808380
With the introduction of CompletableFuture
in Java 8, the world of asynchronouse programming took a massive step forward. But one may wonder the real reason behind its introduction when we already had **Future **interfacesince Java 5. The real motivation behind CompletableFuture was to overcome certain limitations of **Future **interface:
1\. There was no way to make get() call non-blocking in Future.
2\. No support for exception handling.
3\. Chaining of callbacks was not supported.
4\. There was no way to explicitly complete the task.
In this article, we will see some common usage of CompletableFuture
.
First of all to create a new completableFuture
task, simply call the constructor
CompletableFuture<String> task = new CompletableFuture<String>();
Also, you can explicitly create a completed task
CompletableFuture<String> completedTask =
CompletableFuture.completedFuture("Some Value");
Or mark the task as completed by calling the complete
method. All the clients waiting for task to complete will get the specified result.
task.complete("Task completed forcefully");
To get the result of this CompletableFuture
we can call get()
method.
if(completedTask.isDone()){
completedTask.get();
}
#java #completablefuture #parallel-computing #tutorial #asynchronous-programming
https://loizenai.com Programming Tutorial
1618828800
https://grokonez.com/java/java-8/java-8-multiple-completablefutures
Java 8 Multiple CompletableFutures
In previous posts, we have concept of how to use CompletableFutures. This tutorial is about combining multiple CompletableFutures, it is one of the most important and useful abilities of Java 8 asynchronous processing.
Related articles:
<U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)
thenCompose()
can chain 2 CompletableFutures by using the result which is returned from the invoking future.
private static void testCompose() throws InterruptedException, ExecutionException { CompletableFuture<String> future = createCF(2); // inside future CompletableFuture<String> combinedFuture = future.thenCompose(MainApp::calculateCF);
combinedFuture.thenAccept(result -> System.out.println("accept: " + result)); // check results System.out.println("Future result>> " + future.get()); System.out.println("combinedFuture result>> " + combinedFuture.get());
}
private static CompletableFuture<String> calculateCF(String s) {
return CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { System.out.println("> inside new Future"); return "new Completable Future: " + s; } });
}
combinedFuture
uses thenCompose()
method of future
with calculateCF
function to get and modify the result from previous step of future
.
Run the code, the Console shows:
inside future: waiting for detecting index: 2...
inside future: done...
> inside new Future
accept: new Completable Future: 2
Future result>> 2
combinedFuture result>> new Completable Future: 2
More at:
https://grokonez.com/java/java-8/java-8-multiple-completablefutures
Java 8 Multiple CompletableFutures
#java #java8 #completablefutures
https://loizenai.com Programming Tutorial
1618828068
https://grokonez.com/java/java-8/java-8-completablefuture-handle-exception
Java 8 CompletableFuture Handle Exception
Exception handling is important in software development, Java 8 CompletableFuture also provides methods to handle errors when they occurs.
Related article:
CompletableFuture exceptionally()
For the example in previous post, we should use exceptionally()
after chain of thenApply()
methods:
future = future.thenApply(Integer::parseInt) // input String: "not detected"
.thenApply(r -> r * 2 * Math.PI)
.thenApply(s -> "apply>> " + s)
.exceptionally(ex -> "Error: " + ex.getMessage());
Run the code, the result will be:
Error: java.lang.NumberFormatException: For input string: "not detected"
"not detected"
is the String we return to future object before call the first thenApply()
method.
handle
method:
https://grokonez.com/java/java-8/java-8-completablefuture-handle-exception
Java 8 CompletableFuture Handle Exception
#java #java8 #completablefuture #exception
1592702640
Plotly is a plotting ecosystem that allows you to make plots in Python, as well as JavaScript and R. In this series of articles, I’m focusing on plotting with Python libraries.
Plotly has three different Python APIs, giving you a choice of how to drive it:
An object-oriented API that feels similar to Matplotlib
A data-driven API that specifies plots by constructing dictionaries of JSON-like data
A “Plotly Express” API that gives you high-level plotting functions similar to Seaborn
I’ll explore each of these APIs by making the same plot in each one: a grouped bar plot of historical UK election results.
Before we go further, note that you may need to tune your Python environment to get this code to run, including the following.
Running a recent version of Python (instructions for Linux, Mac, and Windows)
Verify you’re running a version of Python that works with these libraries
#python #simplify data visualization
1591412200
Technology has been evolving at an unprecedented rate (and for quite some time now) to keep up with our ever-changing and ever-urgent expectations as consumers. It’s now got to a point where technology can even pre-empt demands so it can better satiate our predilection for instant gratification.
A large part of this fantastic growth can be directly attributed to improvements in microprocessor technology. You need only look into your pockets; at your smartphone to appreciate this fact. And it’s not just phones — entire cities are now turning “smart” thanks to advancements in technology and processor architecture and design.
No surprise then that Software Design and Programming practices have also evolved to extract maximum performance from the machines that serve us.
#parallel-computing #programming-languages #software-development #java #asynchronous