void addCriticalField(Class<?> clazz, String field) { // Lock the write lock criticalFieldsLock.writeLock().lock(); try { // If the class already exists, then add a new field, otherwise // create the hash map field if (!criticalFields.containsKey(field)) { criticalFields.put(field, new HashSet<String>()); } criticalFields.get(field).add(clazz.getName()); } finally { // Unlock criticalFieldsLock.writeLock().unlock(); } }
private boolean hasCriticalField(Class<?> clazz, String field) { // Lock the write lock criticalFieldsLock.readLock().lock(); try { Set<String> classesWithField = criticalFields.get(field); if (classesWithField == null) { return false; } if (!classesWithField.contains(clazz.getName())) { return false; } return true; } finally { criticalFieldsLock.readLock().unlock(); } }
public WebSessionStat getSessionStat(String sessionId, boolean create) { sessionStatLock.readLock().lock(); try { WebSessionStat uriStat = sessionStatMap.get(sessionId); sessionStatLock.readLock().unlock(); sessionStatLock.writeLock().lock(); try { WebSessionStat uriStat = sessionStatMap.get(sessionId); sessionStatLock.writeLock().unlock();
private ProfileEntryStat getProfileEntry(ProfileEntryKey entryKey) { lock.readLock().lock(); try { ProfileEntryStat entryStat = entries.get(entryKey); if (entryStat != null) { return entryStat; } } finally { lock.readLock().unlock(); } lock.writeLock().lock(); try { ProfileEntryStat entryStat = entries.get(entryKey); if (entryStat == null) { entries.put(entryKey, new ProfileEntryStat()); entryStat = entries.get(entryKey); } return entryStat; } finally { lock.writeLock().unlock(); } }
public void setNodeIfNotExist(HostAndPort node) { w.lock(); try { String nodeKey = getNodeKey(node); if (nodes.containsKey(nodeKey)) return; JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), connectionTimeout, soTimeout, null, 0, null); nodes.put(nodeKey, nodePool); } finally { w.unlock(); } }
public void testWeakReadWrite() { Striped<ReadWriteLock> striped = Striped.lazyWeakReadWriteLock(1000); Object key = new Object(); Lock readLock = striped.get(key).readLock(); WeakReference<Object> garbage = new WeakReference<>(new Object()); GcFinalization.awaitClear(garbage); Lock writeLock = striped.get(key).writeLock(); readLock.lock(); assertFalse(writeLock.tryLock()); readLock.unlock(); }
public String cachedValueOf(final String str) { if (str != null && (lengthLimit < 0 || str.length() <= lengthLimit)) { // Return value from cache if available try { lock.readLock().lock(); WeakReference<String> ref = cache.get(str); if (ref != null) { return ref.get(); } } finally { lock.readLock().unlock(); } // Update cache with new content try { lock.writeLock().lock(); WeakReference<String> ref = cache.get(str); if (ref != null) { return ref.get(); } cache.put(str, new WeakReference<>(str)); } finally { lock.writeLock().unlock(); } return str; } return str; }