Java Code Examples for java.util.concurrent.ConcurrentMap#remove()

The following examples show how to use java.util.concurrent.ConcurrentMap#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleVirtualGroupStore.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void removeGroupEntry(NetworkId networkId, Group group) {
    StoredGroupEntry existing = null;
    if (groupEntriesById.get(networkId) != null
            && groupEntriesById.get(networkId).get(group.deviceId()) != null) {
       existing = groupEntriesById
               .get(networkId).get(group.deviceId()).get(group.id());
    }

    if (existing != null) {
        ConcurrentMap<GroupKey, StoredGroupEntry> keyTable =
                getGroupKeyTable(networkId, existing.deviceId());
        ConcurrentMap<GroupId, StoredGroupEntry> idTable =
                getGroupIdTable(networkId, existing.deviceId());
        idTable.remove(existing.id());
        keyTable.remove(existing.appCookie());
        notifyDelegate(networkId,
                       new GroupEvent(GroupEvent.Type.GROUP_REMOVED, existing));
    }
}
 
Example 2
Source File: AbstractZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void removeChildListener(String path, ChildListener listener) {
	ConcurrentMap<ChildListener, TargetChildListener> listeners = childListeners.get(path);
	if (listeners != null) {
		TargetChildListener targetListener = listeners.remove(listener);
		if (targetListener != null) {
			removeTargetChildListener(path, targetListener);
		}
	}
}
 
Example 3
Source File: Thread.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes from the specified map any keys that have been enqueued
 * on the specified reference queue.
 */
static void processQueue(ReferenceQueue<Class<?>> queue,
                         ConcurrentMap<? extends
                         WeakReference<Class<?>>, ?> map)
{
    Reference<? extends Class<?>> ref;
    while((ref = queue.poll()) != null) {
        map.remove(ref);
    }
}
 
Example 4
Source File: ClusterNodeManager.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
protected void removeTopicGroupNode(String topic, int group) {
    ConcurrentMap<Integer, ClusterNode> groupMap = nodeMap.get(topic);
    if (groupMap == null) {
        return;
    }
    logger.info("remove topic group node, topic: {}, group: {}, leader: {}", topic, group, groupMap.get(group));
    groupMap.remove(group);
    if (groupMap.isEmpty()) {
        nodeMap.remove(topic);
    }
}
 
Example 5
Source File: WeakCache.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void expungeFrom(ConcurrentMap<?, ? extends ConcurrentMap<?, ?>> map,
                 ConcurrentMap<?, Boolean> reverseMap) {
    // removing just by key is always safe here because after a CacheKey
    // is cleared and enqueue-ed it is only equal to itself
    // (see equals method)...
    ConcurrentMap<?, ?> valuesMap = map.remove(this);
    // remove also from reverseMap if needed
    if (valuesMap != null) {
        for (Object cacheValue : valuesMap.values()) {
            reverseMap.remove(cacheValue);
        }
    }
}
 
Example 6
Source File: AbstractZookeeperClient.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void removeChildListener(String path, ChildListener listener) {
	ConcurrentMap<ChildListener, TargetChildListener> listeners = childListeners.get(path);
	if (listeners != null) {
		TargetChildListener targetListener = listeners.remove(listener);
		if (targetListener != null) {
			removeTargetChildListener(path, targetListener);
		}
	}
}
 
Example 7
Source File: AbstractRuleRule.java    From light with Apache License 2.0 5 votes vote down vote up
protected void updReqTransform(Map<String, Object> data) throws Exception {
    String ruleClass = (String)data.get("ruleClass");
    OrientGraph graph = ServiceLocator.getInstance().getGraph();
    try {
        graph.begin();
        Vertex rule = graph.getVertexByKey("Rule.ruleClass", ruleClass);
        if(rule != null) {
            rule.setProperty("reqTransforms", data.get("reqTransforms"));
            rule.setProperty("updateDate", data.get("updateDate"));
            Vertex updateUser = graph.getVertexByKey("User.userId", data.get("updateUserId"));
            if(updateUser != null) {
                updateUser.addEdge("Update", rule);
            }
            Map<String, Object> ruleMap = ServiceLocator.getInstance().getMemoryImage("ruleMap");
            ConcurrentMap<String, Map<String, Object>> cache = (ConcurrentMap<String, Map<String, Object>>)ruleMap.get("cache");
            if(cache != null) {
                cache.remove(ruleClass);
            }
        }
        graph.commit();
    } catch (Exception e) {
        logger.error("Exception:", e);
        graph.rollback();
        throw e;
    } finally {
        graph.shutdown();
    }
}
 
Example 8
Source File: Thread.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes from the specified map any keys that have been enqueued
 * on the specified reference queue.
 */
static void processQueue(ReferenceQueue<Class<?>> queue,
                         ConcurrentMap<? extends
                         WeakReference<Class<?>>, ?> map)
{
    Reference<? extends Class<?>> ref;
    while((ref = queue.poll()) != null) {
        map.remove(ref);
    }
}
 
Example 9
Source File: AbstractZookeeperClient.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public synchronized void removeChildListener(String path, ChildListener listener) {
	ConcurrentMap<ChildListener, TargetChildListener> listeners = childListeners.get(path);
	if (listeners != null) {
		TargetChildListener targetListener = listeners.remove(listener);
		if (targetListener != null) {
			removeTargetChildListener(path, targetListener);
		}
		if (listeners.isEmpty()) {
			childListeners.remove(path);
		}
	}
}
 
Example 10
Source File: Thread.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes from the specified map any keys that have been enqueued
 * on the specified reference queue.
 */
static void processQueue(ReferenceQueue<Class<?>> queue,
                         ConcurrentMap<? extends
                         WeakReference<Class<?>>, ?> map)
{
    Reference<? extends Class<?>> ref;
    while((ref = queue.poll()) != null) {
        map.remove(ref);
    }
}
 
Example 11
Source File: ObjectStreamClass.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
/**
 * Removes from the specified map any keys that have been enqueued
 * on the specified reference queue.
 */
static void processQueue(ReferenceQueue<Class<?>> queue,
                         ConcurrentMap<? extends
                         WeakReference<Class<?>>, ?> map)
{
    Reference<? extends Class<?>> ref;
    while((ref = queue.poll()) != null) {
        map.remove(ref);
    }
}
 
Example 12
Source File: perf_hash_test.java    From JCTools with Apache License 2.0 5 votes vote down vote up
public int run_normal(ConcurrentMap<String, String> hm) {
    SimpleRandom R = new SimpleRandom();

    int get_ops = 0;
    int put_ops = 0;
    int del_ops = 0;
    while (!_stop) {
        int x = R.nextInt() & ((1 << 20) - 1);
        String key = KEYS[R.nextInt() & (KEYS.length - 1)];
        if (x < _gr) {
            get_ops++;
            String val = hm.get(key);
            if (val != null && !val.equals(key))
                throw new IllegalArgumentException("Mismatched key=" + key + " and val=" + val);
        }
        else if (x < _pr) {
            put_ops++;
            hm.putIfAbsent(key, key);
        }
        else {
            del_ops++;
            hm.remove(key);
        }
    }
    // We stopped; report results into shared result structure
    return get_ops + put_ops + del_ops;
}
 
Example 13
Source File: WeakCache.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
void expungeFrom(ConcurrentMap<?, ? extends ConcurrentMap<?, ?>> map,
                 ConcurrentMap<?, Boolean> reverseMap) {
    // removing just by key is always safe here because after a CacheKey
    // is cleared and enqueue-ed it is only equal to itself
    // (see equals method)...
    ConcurrentMap<?, ?> valuesMap = map.remove(this);
    // remove also from reverseMap if needed
    if (valuesMap != null) {
        for (Object cacheValue : valuesMap.values()) {
            reverseMap.remove(cacheValue);
        }
    }
}
 
Example 14
Source File: ConcurrentHashMapTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/**
 * remove removes the correct key-value pair from the map
 */
public void testRemove() {
    ConcurrentMap map = map5();
    map.remove(five);
    assertEquals(4, map.size());
    assertFalse(map.containsKey(five));
}
 
Example 15
Source File: ObjectStreamClass.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes from the specified map any keys that have been enqueued
 * on the specified reference queue.
 */
static void processQueue(ReferenceQueue<Class<?>> queue,
                         ConcurrentMap<? extends
                         WeakReference<Class<?>>, ?> map)
{
    Reference<? extends Class<?>> ref;
    while((ref = queue.poll()) != null) {
        map.remove(ref);
    }
}
 
Example 16
Source File: ObjectStreamClass.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes from the specified map any keys that have been enqueued
 * on the specified reference queue.
 */
static void processQueue(ReferenceQueue<Class<?>> queue,
                         ConcurrentMap<? extends
                         WeakReference<Class<?>>, ?> map)
{
    Reference<? extends Class<?>> ref;
    while((ref = queue.poll()) != null) {
        map.remove(ref);
    }
}
 
Example 17
Source File: ObjectStreamClass.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes from the specified map any keys that have been enqueued
 * on the specified reference queue.
 */
static void processQueue(ReferenceQueue<Class<?>> queue,
                         ConcurrentMap<? extends
                         WeakReference<Class<?>>, ?> map)
{
    Reference<? extends Class<?>> ref;
    while((ref = queue.poll()) != null) {
        map.remove(ref);
    }
}
 
Example 18
Source File: WorkerProcessPoolFactory.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an existing WorkerProcessPool for the given job params if one exists, otherwise creates
 * a new one.
 */
public WorkerProcessPool getWorkerProcessPool(
    ExecutionContext context, WorkerProcessParams paramsToUse) {
  ConcurrentMap<String, WorkerProcessPool> processPoolMap;
  String key;
  HashCode workerHash;
  if (paramsToUse.getWorkerProcessIdentity().isPresent()
      && context.getPersistentWorkerPools().isPresent()) {
    processPoolMap = context.getPersistentWorkerPools().get();
    key = paramsToUse.getWorkerProcessIdentity().get().getPersistentWorkerKey();
    workerHash = paramsToUse.getWorkerProcessIdentity().get().getWorkerHash();
  } else {
    processPoolMap = context.getWorkerProcessPools();
    key = Joiner.on(' ').join(getCommand(context.getPlatform(), paramsToUse));
    workerHash = Hashing.sha1().hashString(key, StandardCharsets.UTF_8);
  }

  // If the worker pool has a different hash, recreate the pool.
  WorkerProcessPool pool = processPoolMap.get(key);
  if (pool != null && !pool.getPoolHash().equals(workerHash)) {
    if (processPoolMap.remove(key, pool)) {
      pool.close();
    }
    pool = processPoolMap.get(key);
  }

  if (pool == null) {
    pool = createWorkerProcessPool(context, paramsToUse, processPoolMap, key, workerHash);
  }

  int poolCapacity = pool.getCapacity();
  if (poolCapacity != paramsToUse.getMaxWorkers()) {
    context.postEvent(
        ConsoleEvent.warning(
            "There are two 'worker_tool' targets declared with the same command (%s), but "
                + "different 'max_worker' settings (%d and %d). Only the first capacity is applied. "
                + "Consolidate these workers to avoid this warning.",
            key, poolCapacity, paramsToUse.getMaxWorkers()));
  }

  return pool;
}
 
Example 19
Source File: TreeCache.java    From curator with Apache License 2.0 4 votes vote down vote up
void wasDeleted() throws Exception
{
    ChildData oldChildData = childDataUpdater.getAndSet(this, DEAD);
    if ( oldChildData == DEAD )
    {
        return;
    }
    ConcurrentMap<String, TreeNode> childMap = childrenUpdater.getAndSet(this, null);
    if ( childMap != null )
    {
        ArrayList<TreeNode> childCopy = new ArrayList<TreeNode>(childMap.values());
        childMap.clear();
        for ( TreeNode child : childCopy )
        {
            child.wasDeleted();
        }
    }

    if ( treeState.get() == TreeState.CLOSED )
    {
        return;
    }

    if ( isLive(oldChildData) )
    {
        publishEvent(TreeCacheEvent.Type.NODE_REMOVED, oldChildData, null);
    }

    if ( parent == null )
    {
        // Root node; use an exist query to watch for existence.
        maybeWatch(client.checkExists()).forPath(path);
    }
    else
    {
        // Remove from parent if we're currently a child
        ConcurrentMap<String, TreeNode> parentChildMap = parent.children;
        if ( parentChildMap != null )
        {
            parentChildMap.remove(ZKPaths.getNodeFromPath(path), this);
        }
    }
}
 
Example 20
Source File: ConcurrentMaps.java    From streamsupport with GNU General Public License v2.0 3 votes vote down vote up
/**
 * If the specified key is not already associated with a value or is
 * associated with null, associates it with the given non-null value.
 * Otherwise, replaces the associated value with the results of the given
 * remapping function, or removes if the result is {@code null}. This
 * method may be of use when combining multiple mapped values for a key.
 * For example, to either create or append a {@code String msg} to a
 * value mapping:
 *
 * <pre> {@code
 * map.merge(key, msg, String::concat)
 * }</pre>
 *
 * <p>If the remapping function returns {@code null} the mapping is removed.
 * If the remapping function itself throws an (unchecked) exception, the
 * exception is rethrown, and the current mapping is left unchanged.
 *
 * <p>The remapping function itself should not modify the passed map during computation.
 *
 * <p><b>Implementation Requirements:</b><br>
 * The default implementation is equivalent to performing the following
 * steps for the {@code map}:
 *
 * <pre> {@code
 * for (;;) {
 *   V oldValue = map.get(key);
 *   if (oldValue != null) {
 *     V newValue = remappingFunction.apply(oldValue, value);
 *     if (newValue != null) {
 *       if (map.replace(key, oldValue, newValue))
 *         return newValue;
 *     } else if (map.remove(key, oldValue)) {
 *       return null;
 *     }
 *   } else if (map.putIfAbsent(key, value) == null) {
 *     return value;
 *   }
 * }}</pre>
 * When multiple threads attempt updates, map operations and the
 * remapping function may be called multiple times.
 *
 * <p>This implementation assumes that the ConcurrentMap cannot contain null
 * values and {@code get()} returning null unambiguously means the key is
 * absent. Implementations which support null values <strong>must</strong>
 * override the default implementation.
 *
 * <p>The default implementation makes no guarantees about detecting if the
 * remapping function modifies the passed map during computation and, if
 * appropriate, reporting an error. Concurrent implementations should override
 * this method and, on a best-effort basis, throw an {@code IllegalStateException}
 * if it is detected that the remapping function modifies the map during computation
 * and as a result computation would never complete.
 *
 * @param <K> the type of keys maintained by the passed map
 * @param <V> the type of mapped values in the passed map
 * @param map the {@code ConcurrentMap} on which to execute the {@code merge}
 * operation.
 * @param key key with which the resulting value is to be associated
 * @param value the non-null value to be merged with the existing value
 *        associated with the key or, if no existing value or a null value
 *        is associated with the key, to be associated with the key
 * @param remappingFunction the function to recompute a value if present
 * @return the new value associated with the specified key, or null if no
 *         value is associated with the key
 * @throws UnsupportedOperationException if the {@code put} operation
 *         is not supported by the map (optional)
 * @throws ClassCastException if the class of the specified key or value
 *         prevents it from being stored in the map (optional)
 * @throws NullPointerException if the specified key is null and the map
 *         does not support null keys or the value or remappingFunction is
 *         null
 * @throws IllegalArgumentException if some property of the specified key
 *         or value prevents it from being stored in the map (optional)
 * @since 1.8
 */
public static <K, V> V merge(ConcurrentMap<K, V> map, K key, V value,
        BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
    Objects.requireNonNull(map);
    Objects.requireNonNull(remappingFunction);
    Objects.requireNonNull(value);
    retry: for (;;) {
        V oldValue = map.get(key);
        // if putIfAbsent fails, opportunistically use its return value
        haveOldValue: for (;;) {
            if (oldValue != null) {
                V newValue = remappingFunction.apply(oldValue, value);
                if (newValue != null) {
                    if (map.replace(key, oldValue, newValue))
                        return newValue;
                } else if (map.remove(key, oldValue)) {
                    return null;
                }
                continue retry;
            } else {
                if ((oldValue = map.putIfAbsent(key, value)) == null)
                    return value;
                continue haveOldValue;
            }
        }
    }
}