Java Code Examples for java.util.concurrent.ConcurrentHashMap.clear()

The following are Jave code examples for showing how to use clear() of the java.util.concurrent.ConcurrentHashMap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: SuitAgent   File: CacheByTimeUtil.java   View Source Code Vote up 7 votes
/**
 * 获取缓存数据
 * @param key
 * @return
 * 未设置或缓存有效时间已过都会返回null
 */
public static Object getCache(String key) {
    synchronized (key.intern()){
        if (StringUtils.isNotEmpty(key)){
            ConcurrentHashMap<Long,CacheValue> cache = CATCH.get(key);
            if (cache != null){
                Optional<Long> cacheTime = cache.keySet().stream().findFirst();
                if (cacheTime.isPresent()){
                    CacheValue cacheValue = cache.get(cacheTime.get());
                    long now = System.currentTimeMillis();
                    if ((now - cacheTime.get()) < (cacheValue.getValidTime() * 1000)){
                        return cacheValue.getValue();
                    }else {
                        //缓存失效
                        cache.clear();
                        CATCH.put(key,cache);
                    }
                }
            }
        }
        return null;
    }
}
 
Example 2
Project: GitHub   File: DoubleKeyValueMap.java   View Source Code Vote up 6 votes
public void clear() {
    if (k1_k2V_map.size() > 0) {
        for (ConcurrentHashMap<K2, V> k2V_map : k1_k2V_map.values()) {
            k2V_map.clear();
        }
        k1_k2V_map.clear();
    }
}
 
Example 3
Project: SuitAgent   File: CacheByTimeUtil.java   View Source Code Vote up 6 votes
/**
 * 设置缓存(指定缓存有效时间)
 * @param key
 * @param value
 * @param validTime
 */
public static void setCache(String key,Object value,int validTime) {
    synchronized (key.intern()){
        if (StringUtils.isNotEmpty(key) && value != null){
            ConcurrentHashMap<Long,CacheValue> cache = CATCH.get(key);
            if (cache == null){
                cache = new ConcurrentHashMap<>();
            }
            cache.clear();
            cache.put(System.currentTimeMillis(),new CacheValue(validTime,value));
            CATCH.put(key,cache);
        }
    }
}
 
Example 4
Project: tomcat7   File: StatementCache.java   View Source Code Vote up 6 votes
@Override
public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
    @SuppressWarnings("unchecked")
    ConcurrentHashMap<CacheKey,CachedStatement> statements =
        (ConcurrentHashMap<CacheKey,CachedStatement>)con.getAttributes().get(STATEMENT_CACHE_ATTR);

    if (statements!=null) {
        for (Map.Entry<CacheKey, CachedStatement> p : statements.entrySet()) {
            closeStatement(p.getValue());
        }
        statements.clear();
    }

    super.disconnected(parent, con, finalizing);
}
 
Example 5
Project: apache-tomcat-7.0.73-with-comment   File: StatementCache.java   View Source Code Vote up 6 votes
@Override
public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
    @SuppressWarnings("unchecked")
    ConcurrentHashMap<CacheKey,CachedStatement> statements =
        (ConcurrentHashMap<CacheKey,CachedStatement>)con.getAttributes().get(STATEMENT_CACHE_ATTR);

    if (statements!=null) {
        for (Map.Entry<CacheKey, CachedStatement> p : statements.entrySet()) {
            closeStatement(p.getValue());
        }
        statements.clear();
    }

    super.disconnected(parent, con, finalizing);
}
 
Example 6
Project: openjdk-jdk10   File: ConcurrentHashMapTest.java   View Source Code Vote up 6 votes
/**
 * Maps with same contents are equal
 */
public void testEquals() {
    ConcurrentHashMap map1 = map5();
    ConcurrentHashMap map2 = map5();
    assertEquals(map1, map2);
    assertEquals(map2, map1);
    map1.clear();
    assertFalse(map1.equals(map2));
    assertFalse(map2.equals(map1));
}
 
Example 7
Project: alfresco-repository   File: ReferenceCountingReadOnlyIndexReaderFactory.java   View Source Code Vote up 5 votes
private <T> T manageCache(ConcurrentHashMap<Integer, WithUseCount<T>> cache, Accessor<T> accessor, int n, FieldSelector fieldSelector, int limit) throws IOException
{
    Integer key = Integer.valueOf(n);
    WithUseCount<T> value = cache.get(key);
    if (value == null)
    {
        T made = accessor.get(n, fieldSelector);
        value = new WithUseCount<T>(made, n);
        cache.put(key, value);

        // resize

        if (limit >= 0)
        {
            if (cache.size() >= limit)
            {
                HashMap<Integer, WithUseCount<T>> keep = new HashMap<Integer, WithUseCount<T>>();
                WithUseCount<T>[] existing = new WithUseCount[0];
                synchronized (cache)
                {
                    existing = cache.values().toArray(existing);
                    cache.clear();
                }
                Arrays.sort(existing);

                for (WithUseCount<T> current : existing)
                {
                    keep.put(Integer.valueOf(current.doc), current);
                    if ((current.count.get() == 0) || (keep.size() > (limit / 4)))
                    {
                        break;
                    }
                }
                keep.put(key, value);
                cache.putAll(keep);
            }
        }
    }
    else
    {
        value.count.getAndIncrement();
    }
    return value.object;
}
 
Example 8
Project: metacom-android   File: AndroidJSTPConnection.java   View Source Code Vote up 5 votes
public void removeCachedCalls(String cacheName) {
    ConcurrentHashMap<UUID, CacheCallData> cachedCalls = mTaggedCacheCalls.get(cacheName);
    if (cachedCalls != null) cachedCalls.clear();
}
 
Example 9
Project: openjdk-jdk10   File: ConcurrentHashMapTest.java   View Source Code Vote up 5 votes
/**
 * clear removes all pairs
 */
public void testClear() {
    ConcurrentHashMap map = map5();
    map.clear();
    assertEquals(0, map.size());
}
 
Example 10
Project: pravega-samples   File: SharedMap.java   View Source Code Vote up 5 votes
@Override
public void process(ConcurrentHashMap<K, V> impl) {
    impl.clear();
}