Class ConcurrencyUtilitiesExamples
java.lang.Object
net.jrodolfo.java_evolution.java05.ConcurrencyUtilitiesExamples
Demonstrates
java.util.concurrent, introduced in Java 5.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcountCompletedTasks(ExecutorService executor, int taskCount) Uses a latch to wait for several tasks to finish and an atomic counter to count completions safely.runCallable(ExecutorService executor) Runs a task through anExecutorServiceand obtains the result through aFuture.intsumTaskResults(ExecutorService executor) UsesExecutorService.invokeAll(List)to run related tasks and obtain all results.
-
Constructor Details
-
ConcurrencyUtilitiesExamples
public ConcurrencyUtilitiesExamples()
-
-
Method Details
-
runCallable
Runs a task through anExecutorServiceand obtains the result through aFuture.- Parameters:
executor- executor supplied by the caller- Returns:
- task result
- Throws:
Exception- if the task fails
-
countCompletedTasks
Uses a latch to wait for several tasks to finish and an atomic counter to count completions safely.- Parameters:
executor- executor supplied by the callertaskCount- number of tasks to run- Returns:
- completed task count
- Throws:
InterruptedException- if waiting is interrupted
-
sumTaskResults
UsesExecutorService.invokeAll(List)to run related tasks and obtain all results.- Parameters:
executor- executor supplied by the caller- Returns:
- sum of task results
- Throws:
Exception- if a task fails
-