For this homework, you will create a conditional read-write lock and a thread-safe indexed set using that lock.

Motivation

Your search engine project will allow for multithreaded building and searching of your inverted index data structure. It will be especially important for multiple search operations (read operations) to be able to occur concurrently without blocking. This will require making your inverted index data structure thread-safe using a conditional read-write lock over using the synchronized keyword alone.

Examples

The conditional read-write lock you are creating is similar to (but simpler than) the ReentrantReadWriteLock available in Java. We use a conditional lock around read or write operations to shared data as follows:

ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

// For read operations...

lock.readLock().lock();

try {
  readOperation();
}
finally {
  lock.readLock().unlock();
}

// For write or mixed read/write operations...

lock.writeLock().lock();

try {
  readWriteOperation();
}
finally {
  lock.writeLock().unlock();
}

See the lecture code for additional examples.

Hints

Below are some hints that may help with this homework assignment:

These hints are optional. There may be multiple approaches to solving this homework.