本文整理汇总了Java中java.util.concurrent.ConcurrentMap.replace方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentMap.replace方法的具体用法?Java ConcurrentMap.replace怎么用?Java ConcurrentMap.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ConcurrentMap
的用法示例。
在下文中一共展示了ConcurrentMap.replace方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testReplace
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void testReplace()
{
ConcurrentMap<String, Object> cache = createMap();
_putTwo(cache);
Object val = cache.replace(C_STR, ONE);
assertNull(val);
assertEquals("Replace operation did not work as expected.", 2, cache.size());
assertEquals(ONE, cache.get(A_STR));
assertEquals(TWO, cache.get(B_STR));
val = cache.replace(A_STR, "aaaString");
assertEquals(ONE, val);
assertEquals("Replace operation did not work as expected.", 2, cache.size());
assertEquals("aaaString", cache.get(A_STR));
boolean bool = cache.replace(B_STR, "bb", "newValue");
assertFalse(bool);
assertEquals("Replace operation did not work as expected.", 2, cache.size());
assertEquals(TWO, cache.get(B_STR));
bool = cache.replace(B_STR, TWO, "newValue");
assertTrue(bool);
assertEquals("Replace operation did not work as expected.", 2, cache.size());
assertEquals("newValue", cache.get(B_STR));
}
示例2: cacheLocation
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* Put a newly discovered HRegionLocation into the cache.
* @param tableName The table name.
* @param locations the new locations
*/
public void cacheLocation(final TableName tableName, final RegionLocations locations) {
byte [] startKey = locations.getRegionLocation().getRegionInfo().getStartKey();
ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName);
RegionLocations oldLocation = tableLocations.putIfAbsent(startKey, locations);
boolean isNewCacheEntry = (oldLocation == null);
if (isNewCacheEntry) {
if (LOG.isTraceEnabled()) {
LOG.trace("Cached location: " + locations);
}
addToCachedServers(locations);
return;
}
// merge old and new locations and add it to the cache
// Meta record might be stale - some (probably the same) server has closed the region
// with later seqNum and told us about the new location.
RegionLocations mergedLocation = oldLocation.mergeLocations(locations);
boolean replaced = tableLocations.replace(startKey, oldLocation, mergedLocation);
if (replaced && LOG.isTraceEnabled()) {
LOG.trace("Merged cached locations: " + mergedLocation);
}
addToCachedServers(locations);
}
示例3: clearCache
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* Delete a cached location, no matter what it is. Called when we were told to not use cache.
* @param tableName tableName
* @param row
*/
public void clearCache(final TableName tableName, final byte [] row, int replicaId) {
ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName);
boolean removed = false;
RegionLocations regionLocations = getCachedLocation(tableName, row);
if (regionLocations != null) {
HRegionLocation toBeRemoved = regionLocations.getRegionLocation(replicaId);
RegionLocations updatedLocations = regionLocations.remove(replicaId);
if (updatedLocations != regionLocations) {
byte[] startKey = regionLocations.getRegionLocation().getRegionInfo().getStartKey();
if (updatedLocations.isEmpty()) {
removed = tableLocations.remove(startKey, regionLocations);
} else {
removed = tableLocations.replace(startKey, regionLocations, updatedLocations);
}
}
if (removed && LOG.isTraceEnabled() && toBeRemoved != null) {
LOG.trace("Removed " + toBeRemoved + " from cache");
}
}
}
示例4: testReplaceOldValueFail
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Test
public void testReplaceOldValueFail() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("43"), new SimpleValue("31"));
Assert.assertFalse(res);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
}
示例5: testReplaceOldValueSuccess
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Test
public void testReplaceOldValueSuccess() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertTrue(res);
boolean res1 = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertFalse(res1);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
示例6: testReplaceOldValueFail
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Test
public void testReplaceOldValueFail() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("43"), new SimpleValue("31"));
Assert.assertFalse(res);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("2", val1.getValue());
}
示例7: testReplaceOldValueSuccess
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Test
public void testReplaceOldValueSuccess() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
boolean res = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertTrue(res);
boolean res1 = map.replace(new SimpleKey("1"), new SimpleValue("2"), new SimpleValue("3"));
Assert.assertFalse(res1);
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
示例8: testReplaceValue
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Test
public void testReplaceValue() {
ConcurrentMap<SimpleKey, SimpleValue> map = redisson.getMap("simple");
map.put(new SimpleKey("1"), new SimpleValue("2"));
SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3"));
Assert.assertEquals("2", res.getValue());
SimpleValue val1 = map.get(new SimpleKey("1"));
Assert.assertEquals("3", val1.getValue());
}
示例9: testMapMethods
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void testMapMethods() {
Cache<Integer, Integer> cache = CacheBuilder.newBuilder()
.build();
ConcurrentMap<Integer, Integer> asMap = cache.asMap();
cache.put(10, 100);
cache.put(2, 52);
asMap.replace(2, 79);
asMap.replace(3, 60);
assertEquals(null, cache.getIfPresent(3));
assertEquals(null, asMap.get(3));
assertEquals(Integer.valueOf(79), cache.getIfPresent(2));
assertEquals(Integer.valueOf(79), asMap.get(2));
asMap.replace(10, 100, 50);
asMap.replace(2, 52, 99);
assertEquals(Integer.valueOf(50), cache.getIfPresent(10));
assertEquals(Integer.valueOf(50), asMap.get(10));
assertEquals(Integer.valueOf(79), cache.getIfPresent(2));
assertEquals(Integer.valueOf(79), asMap.get(2));
asMap.remove(10, 100);
asMap.remove(2, 79);
assertEquals(Integer.valueOf(50), cache.getIfPresent(10));
assertEquals(Integer.valueOf(50), asMap.get(10));
assertEquals(null, cache.getIfPresent(2));
assertEquals(null, asMap.get(2));
asMap.putIfAbsent(2, 20);
asMap.putIfAbsent(10, 20);
assertEquals(Integer.valueOf(20), cache.getIfPresent(2));
assertEquals(Integer.valueOf(20), asMap.get(2));
assertEquals(Integer.valueOf(50), cache.getIfPresent(10));
assertEquals(Integer.valueOf(50), asMap.get(10));
}
示例10: get
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* Look-up the value through the cache. This always evaluates the
* {@code subKeyFactory} function and optionally evaluates
* {@code valueFactory} function if there is no entry in the cache for given
* pair of (key, subKey) or the entry has already been cleared.
*
* @param key possibly null key
* @param parameter parameter used together with key to create sub-key and
* value (should not be null)
* @return the cached value (never null)
* @throws NullPointerException if {@code parameter} passed in or
* {@code sub-key} calculated by
* {@code subKeyFactory} or {@code value}
* calculated by {@code valueFactory} is null.
*/
public V get(K key, P parameter) {
Objects.requireNonNull(parameter);
expungeStaleEntries();
Object cacheKey = CacheKey.valueOf(key, refQueue);
// lazily install the 2nd level valuesMap for the particular cacheKey
ConcurrentMap<Object, Supplier<V>> valuesMap = map.get(cacheKey);
if (valuesMap == null) {
ConcurrentMap<Object, Supplier<V>> oldValuesMap
= map.putIfAbsent(cacheKey,
valuesMap = new ConcurrentHashMap<>());
if (oldValuesMap != null) {
valuesMap = oldValuesMap;
}
}
// create subKey and retrieve the possible Supplier<V> stored by that
// subKey from valuesMap
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
Supplier<V> supplier = valuesMap.get(subKey);
Factory factory = null;
while (true) {
if (supplier != null) {
// supplier might be a Factory or a CacheValue<V> instance
V value = supplier.get();
if (value != null) {
return value;
}
}
// else no supplier in cache
// or a supplier that returned null (could be a cleared CacheValue
// or a Factory that wasn't successful in installing the CacheValue)
// lazily construct a Factory
if (factory == null) {
factory = new Factory(key, parameter, subKey, valuesMap);
}
if (supplier == null) {
supplier = valuesMap.putIfAbsent(subKey, factory);
if (supplier == null) {
// successfully installed Factory
supplier = factory;
}
// else retry with winning supplier
} else {
if (valuesMap.replace(subKey, supplier, factory)) {
// successfully replaced
// cleared CacheEntry / unsuccessful Factory
// with our Factory
supplier = factory;
} else {
// retry with current supplier
supplier = valuesMap.get(subKey);
}
}
}
}
示例11: computeIfPresent
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* If the value for the specified key is present and non-null, attempts to
* compute a new mapping given the key and its current mapped value.
*
* <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; (oldValue = map.get(key)) != null; ) {
* V newValue = remappingFunction.apply(key, oldValue);
* if ((newValue == null)
* ? map.remove(key, oldValue)
* : map.replace(key, oldValue, newValue))
* return newValue;
* }
* return null;}</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 this default implementation.
*
* @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 computeIfPresent} operation.
* @param key key with which the specified value is to be associated
* @param remappingFunction the remapping function to compute a value
* @return the new value associated with the specified key, or null if none
* @throws NullPointerException if the specified key is null and
* the map does not support null keys, or the
* remappingFunction is null
* @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 IllegalArgumentException if some property of the specified key
* or value prevents it from being stored in this map (optional)
* @since 1.8
*/
public static <K, V> V computeIfPresent(ConcurrentMap<K, V> map, K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(map);
Objects.requireNonNull(remappingFunction);
for (V oldValue; (oldValue = map.get(key)) != null; ) {
V newValue = remappingFunction.apply(key, oldValue);
if ((newValue == null)
? map.remove(key, oldValue)
: map.replace(key, oldValue, newValue))
return newValue;
}
return null;
}