ConcurrentHashMap get - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for ConcurrentHashMap get method.

This code example shows how to use the following methods:get, putIfAbsent
 
    public void subscribe(final String topic, final String group, final int maxSize,
            final MessageListener messageListener, final ConsumerMessageFilter consumerMessageFilter)
                    throws MetaClientException { 
        final ConcurrentHashMap<String, SubscriberInfo> topicSubsriberRegistry = this.getTopicSubscriberRegistry(group);
        SubscriberInfo info = topicSubsriberRegistry.get(topic);
        if (info == null) {
            info = new SubscriberInfo(messageListener, consumerMessageFilter, maxSize);
            final SubscriberInfo oldInfo = topicSubsriberRegistry.putIfAbsent(topic, info);
            if (oldInfo != null) {
                throw new MetaClientException("Topic=" + topic + " has been subscribered by group " + group);
            } 
        } 
        else { 
            throw new MetaClientException("Topic=" + topic + " has been subscribered by group " + group);
        } 
    } 
 
 
    private ConcurrentHashMap<String, SubscriberInfo> getTopicSubscriberRegistry(final String group)

9
CodeRank
This code example shows how to use the following methods:get, putIfAbsent
    // String is ThreadPoolKey.name() (we can't use ThreadPoolKey directly as we can't guarantee it implements hashcode/equals correctly) 
    private final ConcurrentHashMap<String, HystrixMetricsPublisherThreadPool> threadPoolPublishers = new ConcurrentHashMap<String, HystrixMetricsPublisherThreadPool>();
 
    /* package */ HystrixMetricsPublisherThreadPool getPublisherForThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolMetrics metrics, HystrixThreadPoolProperties properties) {
        // attempt to retrieve from cache first 
        HystrixMetricsPublisherThreadPool publisher = threadPoolPublishers.get(threadPoolKey.name());
        if (publisher != null) {
            return publisher;
        } 
        // it doesn't exist so we need to create it 
        publisher = strategy.getMetricsPublisherForThreadPool(threadPoolKey, metrics, properties);
        // attempt to store it (race other threads) 
        HystrixMetricsPublisherThreadPool existing = threadPoolPublishers.putIfAbsent(threadPoolKey.name(), publisher);
        if (existing == null) {
            // we won the thread-race to store the instance we created so initialize it 
            publisher.initialize();
            // done registering, return instance that got cached 
            return publisher;
        } else { 
            // we lost so return 'existing' and let the one we created be garbage collected 
8
CodeRank
This code example shows how to use the following methods:get, putIfAbsent
	 
	private static final String renderKey = "$renderKey$";
	private static volatile ConcurrentHashMap<String, ReentrantLock> lockMap = new ConcurrentHashMap<String, ReentrantLock>();
	 
	private ReentrantLock getLock(String key) {
		ReentrantLock lock = lockMap.get(key);
		if (lock != null)
			return lock;
		 
		lock = new ReentrantLock();
		ReentrantLock previousLock = lockMap.putIfAbsent(key, lock);
		return previousLock == null ? lock : previousLock;
	} 
	 
	final public void intercept(ActionInvocation ai) {
		Controller controller = ai.getController();
		String cacheName = buildCacheName(ai, controller);
		String cacheKey = buildCacheKey(ai, controller);
		Map<String, Object> cacheData = CacheKit.get(cacheName, cacheKey);
		if (cacheData == null) { 
8
CodeRank
This code example shows how to use the following methods:get, putIfAbsent
     */ 
    public static HystrixCommandProperties getCommandProperties(HystrixCommandKey key, HystrixCommandProperties.Setter builder) { 
        HystrixPropertiesStrategy hystrixPropertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy(); 
        String cacheKey = hystrixPropertiesStrategy.getCommandPropertiesCacheKey(key, builder); 
        if (cacheKey != null) { 
            HystrixCommandProperties properties = commandProperties.get(cacheKey); 
            if (properties != null) { 
                return properties; 
            } else { 
                if (builder == null) { 
                    builder = HystrixCommandProperties.Setter(); 
                } 
                // create new instance 
                properties = hystrixPropertiesStrategy.getCommandProperties(key, builder); 
                // cache and return 
                HystrixCommandProperties existing = commandProperties.putIfAbsent(cacheKey, properties); 
                if (existing == null) { 
                    return properties; 
                } else { 
                    return existing; 
                } 
8
CodeRank
This code example shows how to use the following methods:get, putIfAbsent
     * @return An instance of the AnnotationValidator. 
     * 
     * @since 4.12 
     */ 
    public AnnotationValidator createAnnotationValidator(ValidateWith validateWithAnnotation) {
        AnnotationValidator validator = VALIDATORS_FOR_ANNOTATION_TYPES.get(validateWithAnnotation);
        if (validator != null) {
            return validator;
        } 
 
        Class<? extends AnnotationValidator> clazz = validateWithAnnotation.value();
        if (clazz == null) {
            throw new IllegalArgumentException("Can't create validator, value is null in annotation " + validateWithAnnotation.getClass().getName());
        } 
        try { 
            AnnotationValidator annotationValidator = clazz.newInstance();
            VALIDATORS_FOR_ANNOTATION_TYPES.putIfAbsent(validateWithAnnotation, annotationValidator);
            return VALIDATORS_FOR_ANNOTATION_TYPES.get(validateWithAnnotation);
        } catch (Exception e) {
            throw new RuntimeException("Exception received when creating AnnotationValidator class " + clazz.getName(), e);

8
CodeRank
This code example shows how to use the following methods:get, putIfAbsent, remove
     * @param k key 
     * @return value 
     * @throws InterruptedException if interrupted while waiting 
     */ 
    public V get(K k) throws InterruptedException { 
        map.putIfAbsent(k, new SlotFuture<V>()); 
        V v = Util.safeGet( 
            map.get(k), 
            "Waiting to retrieve a value from BlockingHashMap."); 
        map.remove(k); 
        return v; 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:get, putIfAbsent
 
  /** 
   * Returns the lock object for class loading operations. 
   */ 
  protected Object getClassLoadingLock(String className) { 
    Object lock = parallelLockMap.get(className); 
    if (lock != null) { 
      return lock; 
    } 
 
    Object newLock = new Object(); 
    lock = parallelLockMap.putIfAbsent(className, newLock); 
    if (lock == null) { 
      lock = newLock; 
    } 
    return lock; 
  } 
} 
7
CodeRank
This code example shows how to use the following methods:get, isEmpty, keySet, remove
	 *  
	 * @see org.mixare.mgr.downloader.DownloadManager#getNextResult() 
	 */ 
	public synchronized DownloadResult getNextResult() { 
		DownloadResult result = null;
		if (!doneList.isEmpty()) {
			String nextId = doneList.keySet().iterator().next();
			result = doneList.get(nextId);
			doneList.remove(nextId);
		} 
		return result;
	} 
	 
	/* 
	 * (non-Javadoc) 
	 *  
	 * @see org.mixare.mgr.downloader.DownloadManager#getResultSize() 
	 */ 
	public int getResultSize(){ 
		return doneList.size();
7
CodeRank
This code example shows how to use the following methods:containsKey, get, put
    private static ConcurrentHashMap<String, Object> services = new ConcurrentHashMap<String, Object>();
 
    @SuppressWarnings("unchecked") 
    public T get(final Class<T> cls){
        String key = cls.getName();
        if (services.containsKey(key)) {
            return (T) services.get(key);
 
        } else { 
            T service = createClient(cls, targetIP, targetPort, connectTimeout,clientNums);
            services.put(key, service);
            return (T) services.get(key);
        } 
    } 
 
    protected T createClient(Class<T> cls, String targetIP, int targetPort, int connectTimeout,int clientNums){
        ReferenceConfig<T> referenceConfig = new ReferenceConfig<T>();
        referenceConfig.setInterface(cls);
        StringBuilder url = new StringBuilder();
        url.append("dubbo://");
7
CodeRank
This code example shows how to use the following methods:get, putIfAbsent
      JAXBContext result;
 
      JAXBContext jaxb = null;
      if (type != null)
      { 
         jaxb = cache.get((Class<?>) type);
         if (jaxb != null)
         { 
            return jaxb;
         } 
      } 
      jaxb = findProvidedJAXBContext((Class<?>) type, mediaType);
      if (jaxb != null)
      { 
         cache.putIfAbsent((Class<?>) type, jaxb);
         return jaxb;
      } 
      jaxb = createContext(parameterAnnotations, type);
      if (jaxb != null && type != null) cache.putIfAbsent((Class<?>) type, jaxb);
      result = jaxb;

7
CodeRank
get Method Overview
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
See Code Examples for other Java 8 ConcurrentHashMap Methods: