本文整理汇总了Java中java.util.concurrent.ConcurrentMap.putIfAbsent方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentMap.putIfAbsent方法的具体用法?Java ConcurrentMap.putIfAbsent怎么用?Java ConcurrentMap.putIfAbsent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ConcurrentMap
的用法示例。
在下文中一共展示了ConcurrentMap.putIfAbsent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRegionInterests
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* Return the RegionInterestEntry for the given region. Create one if none exists and forRemoval
* is false.
*
* @param r specified region
* @param interestType desired interest type
* @param forRemoval true if calls wants one for removal
* @return the entry or null if none exists and forRemoval is true.
*/
private RegionInterestEntry getRegionInterests(LocalRegion r, int interestType,
boolean forRemoval, boolean isDurable, boolean receiveUpdatesAsInvalidates) {
final String regionName = r.getFullPath();
ConcurrentMap mapOfInterest =
getRegionToInterestsMap(interestType, isDurable, receiveUpdatesAsInvalidates);
RegionInterestEntry result = (RegionInterestEntry) mapOfInterest.get(regionName);
if (result == null && !forRemoval) {
RegionInterestEntry rie = new RegionInterestEntry(r);
result = (RegionInterestEntry) mapOfInterest.putIfAbsent(regionName, rie);
if (result == null) {
result = rie;
}
}
return result;
}
示例2: subscribe
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void subscribe(URL url, NotifyListener listener) {
if (getUrl().getPort() == 0) {
URL registryUrl = RpcContext.getContext().getUrl();
if (registryUrl != null && registryUrl.getPort() > 0
&& RegistryService.class.getName().equals(registryUrl.getPath())) {
super.setUrl(registryUrl);
super.register(registryUrl);
}
}
String client = RpcContext.getContext().getRemoteAddressString();
ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
if (clientListeners == null) {
remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
clientListeners = remoteSubscribed.get(client);
}
Set<NotifyListener> listeners = clientListeners.get(url);
if (listeners == null) {
clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
listeners = clientListeners.get(url);
}
listeners.add(listener);
super.subscribe(url, listener);
subscribed(url, listener);
}
示例3: applicationAttemptStarted
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Override
public void applicationAttemptStarted(
ApplicationAttemptStartData appAttemptStart) throws IOException {
ConcurrentMap<ApplicationAttemptId, ApplicationAttemptHistoryData> subMap =
getSubMap(appAttemptStart.getApplicationAttemptId().getApplicationId());
ApplicationAttemptHistoryData oldData =
subMap.putIfAbsent(appAttemptStart.getApplicationAttemptId(),
ApplicationAttemptHistoryData.newInstance(
appAttemptStart.getApplicationAttemptId(),
appAttemptStart.getHost(), appAttemptStart.getRPCPort(),
appAttemptStart.getMasterContainerId(), null, null, null, null));
if (oldData != null) {
throw new IOException("The start information of application attempt "
+ appAttemptStart.getApplicationAttemptId() + " is already stored.");
}
}
示例4: containerNeed
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
private AtomicInteger containerNeed(TaskId taskID) {
JobId jobID = taskID.getJobId();
TaskType taskType = taskID.getTaskType();
ConcurrentMap<JobId, AtomicInteger> relevantMap
= taskType == TaskType.MAP ? mapContainerNeeds : reduceContainerNeeds;
AtomicInteger result = relevantMap.get(jobID);
if (result == null) {
relevantMap.putIfAbsent(jobID, new AtomicInteger(0));
result = relevantMap.get(jobID);
}
return result;
}
示例5: testPutIfAbsent
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void testPutIfAbsent()
{
ConcurrentMap<String, Object> cache = createMap();
Object val = cache.putIfAbsent(A_STR, ONE);
assertEquals("putIfAbsent operation did not work as expected.", 1, cache.size());
assertNull(val);
val = cache.putIfAbsent(A_STR, "newVal");
assertEquals(ONE, val);
val = cache.putIfAbsent(B_STR, TWO);
assertNull(val);
assertEquals("putIfAbsent operation did not work as expected.", 2, cache.size());
}
示例6: get
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
private static NashornCallSiteDescriptor get(final MethodHandles.Lookup lookup, final String operator, final String operand, final MethodType methodType, final int flags) {
final NashornCallSiteDescriptor csd = new NashornCallSiteDescriptor(lookup, operator, operand, methodType, flags);
// Many of these call site descriptors are identical (e.g. every getter for a property color will be
// "dyn:getProp:color(Object)Object", so it makes sense canonicalizing them.
final ConcurrentMap<NashornCallSiteDescriptor, NashornCallSiteDescriptor> classCanonicals = canonicals.get(lookup.lookupClass());
final NashornCallSiteDescriptor canonical = classCanonicals.putIfAbsent(csd, csd);
return canonical != null ? canonical : csd;
}
示例7: getCacheManager
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Override
public CacheManager getCacheManager(URI uri, ClassLoader classLoader, Properties properties) {
if (uri == null) {
uri = getDefaultURI();
}
if (uri == null) {
throw new CacheException("Uri is not defined. Can't load default configuration");
}
if (classLoader == null) {
classLoader = getDefaultClassLoader();
}
ConcurrentMap<URI, CacheManager> value = new ConcurrentHashMap<URI, CacheManager>();
ConcurrentMap<URI, CacheManager> oldValue = managers.putIfAbsent(classLoader, value);
if (oldValue != null) {
value = oldValue;
}
CacheManager manager = value.get(uri);
if (manager != null) {
return manager;
}
Config config = loadConfig(uri);
Redisson redisson = null;
if (config != null) {
redisson = (Redisson) Redisson.create(config);
}
manager = new JCacheManager(redisson, classLoader, this, properties, uri);
CacheManager oldManager = value.putIfAbsent(uri, manager);
if (oldManager != null) {
if (redisson != null) {
redisson.shutdown();
}
manager = oldManager;
}
return manager;
}
示例8: testSingleRemoveValue_SingleInstance
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
@Test
public void testSingleRemoveValue_SingleInstance() throws InterruptedException {
final String name = "testSingleRemoveValue_SingleInstance";
ConcurrentMap<String, String> map = BaseTest.createInstance().getMap(name);
map.putIfAbsent("1", "0");
testSingleInstanceConcurrency(100, r -> {
ConcurrentMap<String, String> map1 = r.getMap(name);
map1.remove("1", "0");
});
assertMapSize(0, name);
}
示例9: addChildListener
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public List<String> addChildListener(String path, final ChildListener listener) {
ConcurrentMap<ChildListener, TargetChildListener> listeners = childListeners.get(path);
if (listeners == null) {
childListeners.putIfAbsent(path, new ConcurrentHashMap<ChildListener, TargetChildListener>());
listeners = childListeners.get(path);
}
TargetChildListener targetListener = listeners.get(listener);
if (targetListener == null) {
listeners.putIfAbsent(listener, createTargetChildListener(path, listener));
targetListener = listeners.get(listener);
}
return addTargetChildListener(path, targetListener);
}
示例10: __getCachableServices
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* Gets all the registered services of the supplied type. Once instantiated, the services are
* cached in the application scoped storage, and any further requests to this function will
* get the cached services. It is assumed that the name of the service file and the type of
* the service class is the same.
* @param service The class type of the service
* @return The list of registered service instances
*/
static <T> List<T> __getCachableServices(Class<T> service)
{
List<T> services = Collections.emptyList();
String serviceName = service.getName();
ConcurrentMap<String, Object> concurrentAppMap =
RequestContext.getCurrentInstance().getApplicationScopedConcurrentMap();
Object serviceObject = concurrentAppMap.get(serviceName);
if (serviceObject == null)
{
services = ClassLoaderUtils.getServices(service);
Object oldValue = concurrentAppMap.putIfAbsent(serviceName, services);
// if a different thread raced us and added one, use that
if (oldValue != null)
{
services = (List<T>) oldValue;
}
}
else
{
services = (List<T>) serviceObject;
}
return services;
}
示例11: getField
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
private static Field getField(Class<?> cls, String fieldName) {
Field result = null;
if (CLASS_FIELD_CACHE.containsKey(cls)
&& CLASS_FIELD_CACHE.get(cls).containsKey(fieldName)) {
return CLASS_FIELD_CACHE.get(cls).get(fieldName);
}
try {
result = cls.getField(fieldName);
} catch (NoSuchFieldException e) {
for(Field field : cls.getFields()) {
if (fieldName.equals(field.getName())
&& ReflectUtils.isPublicInstanceField(field)) {
result = field;
break;
}
}
}
if (result != null) {
ConcurrentMap<String, Field> fields = CLASS_FIELD_CACHE.get(cls);
if (fields == null) {
fields = new ConcurrentHashMap<String, Field>();
CLASS_FIELD_CACHE.putIfAbsent(cls, fields);
}
fields = CLASS_FIELD_CACHE.get(cls);
fields.putIfAbsent(fieldName, result);
}
return result;
}
示例12: updateServerStats
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
public void updateServerStats(ServerName serverName, byte[] regionName,
Object r) {
if (!(r instanceof Result)) {
return;
}
Result result = (Result) r;
ClientProtos.RegionLoadStats stats = result.getStats();
if(stats == null){
return;
}
String name = serverName.getServerName() + "," + Bytes.toStringBinary(regionName);
ConcurrentMap<byte[], RegionStats> rsStats = null;
if (serverStats.containsKey(serverName)) {
rsStats = serverStats.get(serverName);
} else {
rsStats = serverStats.putIfAbsent(serverName,
new ConcurrentSkipListMap<byte[], RegionStats>(Bytes.BYTES_COMPARATOR));
if (rsStats == null) {
rsStats = serverStats.get(serverName);
}
}
RegionStats regionStats = null;
if (rsStats.containsKey(regionName)) {
regionStats = rsStats.get(regionName);
} else {
regionStats = rsStats.putIfAbsent(regionName, new RegionStats(this.registry, name));
if (regionStats == null) {
regionStats = rsStats.get(regionName);
}
}
regionStats.update(stats);
}
示例13: jsonNodeToAllocationPools
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* Changes JsonNode alocPools to a collection of the alocPools.
*
* @param allocationPools the allocationPools JsonNode
* @return a collection of allocationPools
*/
public Iterable<AllocationPool> jsonNodeToAllocationPools(JsonNode allocationPools) {
checkNotNull(allocationPools, JSON_NOT_NULL);
ConcurrentMap<Integer, AllocationPool> alocplMaps = Maps
.newConcurrentMap();
Integer i = 0;
for (JsonNode node : allocationPools) {
IpAddress startIp = IpAddress.valueOf(node.get("start").asText());
IpAddress endIp = IpAddress.valueOf(node.get("end").asText());
AllocationPool alocPls = new DefaultAllocationPool(startIp, endIp);
alocplMaps.putIfAbsent(i, alocPls);
i++;
}
return Collections.unmodifiableCollection(alocplMaps.values());
}
示例14: _getCompatibleEntry
import java.util.concurrent.ConcurrentMap; //导入方法依赖的package包/类
/**
* Look in the entry cache for a compatible entry.
* A compatible entry is one with the same DerivationKey, which is essentially the
* same StyleSheetNodes.
*/
private Entry _getCompatibleEntry(
ConcurrentMap<Key, Future<Entry>> cache,
Key key,
DerivationKey derivationKey,
ConcurrentMap<Object, Entry> entryCache,
boolean checkModified
)
{
Entry entry = entryCache.get(derivationKey);
if ((entry != null) && !_validateEntry(entry, checkModified))
{
entryCache.remove(derivationKey, entry);
entry = null;
}
if (entry != null)
{
// If we've find a matching entry, store it in the main Key-based cache.
cache.putIfAbsent(key, new ResolvedFuture<Entry>(entry));
// If the cache already contains an entry (ie. putIfAbsent returns a
// non-null value), this means that the Key-based cached and the
// DerivationKey-based cache will contain different Entry instances.
// This is somewhat unexpected but not necessarily fatal, so we don't
// take any special action for this case.
}
return entry;
}
示例15: 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));
}