/** * 设置上下文信息(不存在才设置成功) * * @param key the key * @param value the value * @return the object * @see ConcurrentHashMap#putIfAbsent(Object, Object) */ public static Object putIfAbsent(String key, Object value) { return value == null ? CONTEXT.remove(key) : CONTEXT.putIfAbsent(key, value); }
/** * Get shared connection */ private ExchangeClient getSharedClient(URL url) { String key = url.getAddress(); ReferenceCountExchangeClient client = referenceClientMap.get(key); if (client != null) { if (!client.isClosed()) { client.incrementAndGetCount(); return client; } else { referenceClientMap.remove(key); } } locks.putIfAbsent(key, new Object()); synchronized (locks.get(key)) { if (referenceClientMap.containsKey(key)) { return referenceClientMap.get(key); } ExchangeClient exchangeClient = initClient(url); client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap); referenceClientMap.put(key, client); ghostClientMap.remove(key); locks.remove(key); return client; } }
public synchronized boolean remove(K k, K v) { List<V> list = cache.get(k); if (list == null) { return false; } if (list.isEmpty()) { cache.remove(k); return false; } boolean removed = list.remove(v); if (removed) { if (list.isEmpty()) { cache.remove(k); } else { list = new ArrayList<V>(list); cache.put(k, list); } } return removed; }
static Semaphore getLock(ConcurrentMap<String, Semaphore> locks2, String key) { Semaphore lock = locks2.get(key); if (lock == null) { Semaphore newLock = new Semaphore(1, true); locks2.get(null); locks2.put(null, null); locks2.remove(null); locks2.containsKey(null); locks2.containsValue(null); locks2.putIfAbsent(null, null); locks2.remove(null, null); locks2.replace(null, null); locks2.replace(null, null, null); lock = locks2.putIfAbsent(key, lock); // value, being null, will *always* throw NullPointerException if (lock == null) lock = newLock; } return lock; } // static Semaphore getLock(ConcurrentMap<String, Semaphore> locks2,
assertNull(map.put(one, two)); assertSame(two, map.get(one)); map.putAll(ImmutableMap.of(two, three)); assertSame(three, map.get(two)); assertSame(two, map.putIfAbsent(one, three)); assertSame(two, map.get(one)); assertNull(map.putIfAbsent(three, one)); assertSame(one, map.get(three)); assertSame(two, map.replace(one, three)); assertTrue(map.containsKey(one)); assertTrue(map.containsValue(one)); assertSame(one, map.remove(one)); assertEquals(0, map.size()); assertFalse(map.remove(one, two)); assertTrue(map.remove(one, one)); assertEquals(0, map.size()); Map<Object, Object> newMap = ImmutableMap.of(one, one); assertEquals(newMap, map); assertEquals(newMap.entrySet(), map.entrySet()); assertEquals(newMap.keySet(), map.keySet()); Set<Object> expectedValues = ImmutableSet.of(one);
private void runTimelineCallbacks(final Function<TimelineCallback, CallbackAction> function) { for (Map.Entry<TimelineCallback, Executor> entry : timelineCallbacks.entrySet()) { entry.getValue().execute( () -> { if (CallbackAction.UNREGISTER == function.apply(entry.getKey())) { timelineCallbacks.remove(entry.getKey()); } } ); } }
private void addJob(Object jobId, StandardScannerExecutor executor) { for (Map.Entry<Object,StandardScannerExecutor> jobs : runningJobs.entrySet()) { StandardScannerExecutor exe = jobs.getValue(); if (exe.isDone() || exe.isCancelled()) { runningJobs.remove(jobs.getKey(),exe); } } runningJobs.putIfAbsent(jobId,executor); Preconditions.checkArgument(runningJobs.get(jobId)==executor,"Another job with the same id is already running: %s",jobId); }
private void addJob(Object jobId, StandardScannerExecutor executor) { for (Map.Entry<Object,StandardScannerExecutor> jobs : runningJobs.entrySet()) { StandardScannerExecutor exe = jobs.getValue(); if (exe.isDone() || exe.isCancelled()) { runningJobs.remove(jobs.getKey(),exe); } } Preconditions.checkArgument(runningJobs.putIfAbsent(jobId, executor) == null,"Another job with the same id is already running: %s",jobId); }
private void cleanUp() { Instant now = Instant.now(); if (lastCleanup.plus(cleanupInterval).isAfter(now)) { return; } lastCleanup = now; for (Entry<String, NotificationFilter> entry : getNotificationFilters().entrySet()) { if (entry.getValue() instanceof ExpiringNotificationFilter && ((ExpiringNotificationFilter) entry.getValue()).isExpired()) { LOGGER.debug("Expired filter '{}' removed", entry); filters.remove(entry.getKey()); } } }
/** * Get shared connection */ private ExchangeClient getSharedClient(URL url) { String key = url.getAddress(); ReferenceCountExchangeClient client = referenceClientMap.get(key); if (client != null) { if (!client.isClosed()) { client.incrementAndGetCount(); return client; } else { referenceClientMap.remove(key); } } locks.putIfAbsent(key, new Object()); synchronized (locks.get(key)) { if (referenceClientMap.containsKey(key)) { return referenceClientMap.get(key); } ExchangeClient exchangeClient = initClient(url); client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap); referenceClientMap.put(key, client); ghostClientMap.remove(key); locks.remove(key); return client; } }
@Override public void peerSyncReplicationStateChange(String peerId, SyncReplicationState from, SyncReplicationState to, int stage) { if (from == SyncReplicationState.ACTIVE) { if (stage == 0) { Lock lock = createLock.acquireLock(peerId); try { Optional<DualAsyncFSWAL> opt = peerId2WAL.get(peerId); if (opt != null) { opt.ifPresent(w -> w.skipRemoteWAL(to == SyncReplicationState.STANDBY)); } else { // add a place holder to tell the getWAL caller do not use DualAsyncFSWAL any more. peerId2WAL.put(peerId, Optional.empty()); } } finally { lock.unlock(); } } else if (stage == 1) { peerId2WAL.remove(peerId).ifPresent(this::safeClose); } } }
assertNull(map.put(one, one)); assertSame(one, map.get(one)); assertTrue(map.containsKey(one)); assertTrue(map.containsValue(one)); Object three = new Object(); assertTrue(map.replace(one, two, three)); assertTrue(map.remove(one, three)); assertFalse(map.containsKey(one)); assertFalse(map.containsValue(one)); assertNull(map.putIfAbsent(two, three)); assertSame(three, map.remove(two)); assertNull(map.put(three, one)); assertNull(map.put(one, two));
assertNull(map.put(one, two)); assertSame(two, map.get(one)); map.putAll(ImmutableMap.of(two, three)); assertSame(three, map.get(two)); assertSame(two, map.putIfAbsent(one, three)); assertSame(two, map.get(one)); assertNull(map.putIfAbsent(three, one)); assertSame(one, map.get(three)); assertSame(two, map.replace(one, three)); assertTrue(map.containsKey(one)); assertTrue(map.containsValue(one)); assertSame(one, map.remove(one)); assertEquals(0, map.size()); assertFalse(map.remove(one, two)); assertTrue(map.remove(one, one)); assertEquals(0, map.size()); Map<Object, Object> newMap = ImmutableMap.of(one, one); assertEquals(newMap, map); assertEquals(newMap.entrySet(), map.entrySet()); assertEquals(newMap.keySet(), map.keySet()); Set<Object> expectedValues = ImmutableSet.of(one);
/** * 设置上下文信息(不存在才设置成功) * * @param key the key * @param value the value * @return the object * @see ConcurrentHashMap#putIfAbsent(Object, Object) */ public static Object putIfAbsent(String key, Object value) { return value == null ? CONTEXT.remove(key) : CONTEXT.putIfAbsent(key, value); }
private void runServerRemovedCallbacks(final DruidServer server) { for (final Map.Entry<ServerRemovedCallback, Executor> entry : serverRemovedCallbacks.entrySet()) { entry.getValue().execute( () -> { if (CallbackAction.UNREGISTER == entry.getKey().serverRemoved(server)) { serverRemovedCallbacks.remove(entry.getKey()); } } ); } }
@Override public void deregisterCacheEntryListener(CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration) { Map<Integer, String> listenerIds = listeners.remove(cacheEntryListenerConfiguration); if (listenerIds != null) { for (Map.Entry<Integer, String> entry : listenerIds.entrySet()) { redisson.getTopic(entry.getValue()).removeListener(entry.getKey()); } } config.removeCacheEntryListenerConfiguration(cacheEntryListenerConfiguration); }