Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- 10-11. Executing Multiple Tasks Asynchronously Problem
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 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 |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling