Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet200/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   196   197   198   199   200   201   202   203   ...   245
Bog'liq
Java 17 Recipes

 How It Works
Prior to Java 8, it was important to utilize atomic numbers when working with values 
across multiple threads. Atomic variables prevent thread interference without 
obstructing the way that synchronized access may cause in some cases. Java 8 
introduced a new line of atomic variables that provide faster throughput than standard 
atomic variables. The java.util.concurrent.atomic.DoubleAdder and java.util 
.concurrent.atomic.LongAdder classes are preferable to AtomicDouble and AtomicLong 
in most cases when the values may be accessed and updated across multiple threads. 
Both DoubleAdder and LongAdder extend numbers, and they are useful when summing 
values across threads, especially under high contention.
In the solution, DoubleAdder sums numbers across two different threads. Using the 
add() method, various numbers are “added” to the DoubleAdder value. After the threads 
have had ample time to perform their work, the doubleValue() method is called on to 
return the sum of all values as a double.
Both the DoubleAdder and LongAdder classes contain similar methods, although the 
LongAdder does contain a couple of additional helper methods for incrementing and 
decrementing the value of the adder.
10-11. Executing Multiple Tasks Asynchronously
 Problem
Your application requires multiple tasks to be performed simultaneously in an 
asynchronous manner, such that none of the tasks block one another.
Chapter 10 ConCurrenCy


392
 Solution
Utilize CompletableFuture objects to represent the state of each task that is currently 
being performed. Each CompletableFuture object runs on a designated or application- 
determined background thread, issuing a callback to the original calling method once 
completed.
In the following solution, two long-running tasks are invoked by a calling method
and they each utilize the CompletableFuture to report status once the task has been 
completed.
public class Recipe10_11 {
public static void main(String[] args) {
try {
CompletableFuture tasks = performWork()
.thenApply(work -> {
String newTask = work + " Second task complete!";
System.out.println(newTask);
return newTask;
}).thenApply(finalTask -> finalTask + " Final Task 
Complete!");
CompletableFuture future = performSecondWork("Java 9 is 
Great! ");
while(!tasks.isDone()){
System.out.println(future.get());
}
System.out.println(tasks.get());
} catch (ExecutionException | InterruptedException ex) {
}
}
/**
* Returns a CompleableFuture object.
* @return
*/
public static CompletableFuture performWork() {
CompletableFuture resultingWork = CompletableFuture.supplyAsync(
() -> {
Chapter 10 ConCurrenCy


393
String taskMessage = "First task complete!";
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
System.out.println(taskMessage);
return taskMessage;
});
return resultingWork;
}
/**
* Accepts a String and returns a CompletableFuture.
* @param message
* @return
*/
public static CompletableFuture performSecondWork(String message) {
CompletableFuture resultingWork = CompletableFuture.supplyAsync(
() -> {
String taskMessage = message + " Another task complete!";
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
return taskMessage;
});
return resultingWork;
}
}
The following are the results.
First task complete!
First task complete! Second task complete!
Java 9 is Great! Another task complete!
First task complete! Second task complete! Final Task Complete!
Chapter 10 ConCurrenCy


394

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   196   197   198   199   200   201   202   203   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling