I. Repo Structure

jpf-examples/ ├── pom.xml ├── src/ │ ├── main/ │ │ └── java/ │ │ └── com/ │ │ └── example/ │ │ ├── distributedcache/ │ │ │ ├── DistributedCache.java │ │ │ ├── InvalidateTask.java │ │ │ └── ReadTask.java │ │ └── pubsub/ │ │ ├── MessageProcessingPubSub.java │ │ ├── Producer.java │ │ └── Consumer.java └── README.md

II. DistributedCache Problem

In many real-world applications, a shared cache resource is accessed by multiple threads performing different operations. The typical operations in our scenario include:

In my design, I intentionally introduce a deadlock condition by using two distinct ReentrantReadWriteLock instances:

When two threads invoke these operations concurrently, each thread may end up waiting indefinitely for the other lock to be released, thereby causing a deadlock.

1. Architectural Overview

1.1. Key Components

I structured the example into several components, each serving a specific purpose:

1.2. Locking Strategy