Class ConcurrencyUtilitiesExamples

java.lang.Object
net.jrodolfo.java_evolution.java05.ConcurrencyUtilitiesExamples

public class ConcurrencyUtilitiesExamples extends Object
Demonstrates java.util.concurrent, introduced in Java 5.
  • Constructor Details

    • ConcurrencyUtilitiesExamples

      public ConcurrencyUtilitiesExamples()
  • Method Details

    • runCallable

      public String runCallable(ExecutorService executor) throws Exception
      Runs a task through an ExecutorService and obtains the result through a Future.
      Parameters:
      executor - executor supplied by the caller
      Returns:
      task result
      Throws:
      Exception - if the task fails
    • countCompletedTasks

      public int countCompletedTasks(ExecutorService executor, int taskCount) throws InterruptedException
      Uses a latch to wait for several tasks to finish and an atomic counter to count completions safely.
      Parameters:
      executor - executor supplied by the caller
      taskCount - number of tasks to run
      Returns:
      completed task count
      Throws:
      InterruptedException - if waiting is interrupted
    • sumTaskResults

      public int sumTaskResults(ExecutorService executor) throws Exception
      Uses ExecutorService.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