How can I synchronize access to my service object so that multiple clients may have read-only access, but a client that needs to write gets exclusive access?

Tim Rohaly

Jini 1.1 includes a utility class called com.sun.jini.thread.ReadersWriter which implements reader-writer locks. You may use this class as a barrier - surrounding all critical sections in your accessor code by readLock()/readUnlock() and all critical sections in your mutator code by writeLock()/writeUnlock().

This pattern, "Readers and Writers", is used in the common situation where you need to prevent simultaneous reads and writes but want to allow multiple simultaneous reads. Straight synchronization of the accessor and mutator methods would not allow this - it would exclude multiple read methods from executing concurrently, creating a thread-safe object but drastically decreasing the object's liveness.