Using the ExecutorService to Manage Threads that Execute PrintTasks

Figure J.13 uses an ExecutorService object to manage threads that execute PrintTasks (as defined in Fig. J.12). Lines 11–13 create and name three PrintTasks to execute. Line 18 uses Executors method newCachedThreadPool to obtain an ExecutorService that’s capable of creating new threads as they’re needed by the application. These threads are used by ExecutorService (threadExecutor) to execute the Runnables.

 1   // Fig. J.13: TaskExecutor.java 2   // Using an ExecutorService to execute Runnables. 3   import java.util.concurrent.Executors; 4   import java.util.concurrent.ExecutorService; 5  6   public class TaskExecutor 7   { 8      public static ...

Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.