Livelock – Concurrency: Part I

Livelock Two threads may result in a livelock, if the response by a thread to another thread’s action is always to undo or revert the consequences of the action. The threads are responding to each other, but are blocked in making any progress. The combined actions of the threads make it impossible for them to

Starvation – Concurrency: Part I

Starvation A thread can starve because it is unsuccessfully waiting for its turn to be able to proceed with its execution. In addition to deadlock and livelock that result in starvation, some other situations where starvation can occur are the following: Example 22.11 provides another example of starvation. The Hole class at (1) provides the

Utility Classes TimeUnit and ThreadLocalRandom – Concurrency: Part II

23.1 Utility Classes TimeUnit and ThreadLocalRandom This section can be skipped on the first reading, and can be read if and when the need arises while working through this chapter. Before diving into high-level concurrency support provided by the java.util.concurrent package, we begin by providing an overview of two utility classes whose methods are used

The ThreadLocalRandom Class – Concurrency: Part II

The ThreadLocalRandom Class Some examples in this chapter make use of a pseudorandom generator. The class java.util.concurrent.ThreadLocalRandom provides a pseudorandom generator that is confined to the current thread—that is, it is local to the thread. The ThreadLocalRandom class extends the java.util.Random class, and provides methods that return uniformly distributed numerical values. The ThreadLocalRandom class is

The Executor Framework – Concurrency: Part II

23.2 The Executor Framework Executors provide a high-level approach to launching tasks and managing threads. Tasks are executed asynchronously by threads in a multithreaded environment, where a task defines a unit of work—that is, a task is a set of instructions executed by a thread. Internally, an executor maintains a thread pool that utilizes a

The Executors Class – Concurrency: Part II

The Executors Class The java.util.concurrent.Executors class provides factory and utility methods for the interfaces defined in the java.util.concurrent package. In particular, it provides factory methods that return versatile implementations of the ExecutorService (p. 1427) and the ScheduledExecutorService (p. 1440) interfaces shown in Figure 23.1. The executor services created by the methods of the Executors class

The ExecutorService Interface – Concurrency: Part II

The ExecutorService Interface Table 23.2 lists the executor services provided by the factory methods of the Executors class. An executor service implements the ExecutorService interface that extends the Executor interface with methods that provide the following functionality: In addition to the execute() method, an executor service provides the overloaded methods submit(), invokeAll(), and invokeAny() for

Using an Executor Service 2 – Concurrency: Part II

When the executor service is no longer needed, it should be closed properly so that resources associated with the executor service can be reclaimed. As an executor service is not AutoCloseable, we cannot use the try-with-resources statement. The simplest shutdown procedure involves either calling the shutdown() or shutdownNow() method for this purpose. Calling either of

Using an Executor Service – Concurrency: Part II

Using an Executor Service The idiom for using an executor service is embodied in the following steps that are illustrated in Example 23.1: • Create the executor service. • Submit tasks to the executor service. • Shut down and terminate the executor service. Example 23.1 Executor Lifecycle Click here to view code image package executors;import
BACK TO TOP