Using the ExecutorService to Manage Threads that Execute PrintTasks

Figure 23.4 uses an ExecutorService object to manage threads that execute PrintTasks (as defined in Fig. 23.3). 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 to execute the Runnables.

1. For detailed information on handling thread interruptions, see Chapter 7 of Java Concurrency in Practice by Brian Goetz, et al., Addison-Wesley Professional, 2006.

 1   // Fig. 23.4: TaskExecutor.java 2   // Using an ExecutorService to execute Runnables.

Get Java™ How To Program (Early Objects), Tenth 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.