@@ -0,0 +1,873 @@
+class CachedData %7B%0A   Object data;%0A   volatile boolean cacheValid;%0A   final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();%0A%0A   void processCachedData() %7B%0A     rwl.readLock().lock();%0A     if (!cacheValid) %7B%0A       // Must release read lock before acquiring write lock%0A       rwl.readLock().unlock();%0A       rwl.writeLock().lock();%0A       try %7B%0A         // Recheck state because another thread might have%0A         // acquired write lock and changed state before we did.%0A         if (!cacheValid) %7B%0A           data = ...%0A           cacheValid = true;%0A         %7D%0A         // Downgrade by acquiring read lock before releasing write lock%0A         rwl.readLock().lock();%0A       %7D finally %7B%0A         rwl.writeLock().unlock(); // Unlock write, still hold read%0A       %7D%0A     %7D%0A%0A     try %7B%0A       use(data);%0A     %7D finally %7B%0A       rwl.readLock().unlock();%0A     %7D%0A   %7D%0A %7D%0A

Create sample.java