4.2. Using Multiple Semaphores

In many distributed systems, synchronization needs to go far beyond what can be handled by a single semaphore. Often, many different kinds of resources come into play, and the system must control access to each kind, whether through semaphores or other techniques. But even when semaphores are in place to coordinate access to all resources, that isn't always enough by itself to prevent synchronization problems.

Consider the following example:

public aThenB() {
  //... obtain resource A
  //... obtain resource B
  //... compute
}

public bThenA() {
  //... obtain resource B
  //... obtain resource A
  //... compute
}

Suppose Process 1 executes the aThenB method, while Process 2 executes bThenA. The following interleaved execution ...

Get JavaSpaces™ Principles, Patterns, and Practice 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.