本文整理了Java中java.util.concurrent.ConcurrentHashMap.putIfAbsent()
方法的一些代码示例,展示了ConcurrentHashMap.putIfAbsent()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ConcurrentHashMap.putIfAbsent()
方法的具体详情如下:
包路径:java.util.concurrent.ConcurrentHashMap
类名称:ConcurrentHashMap
方法名:putIfAbsent
暂无
代码示例来源:origin: Netflix/zuul
protected Deque<PooledConnection> getPoolForEventLoop(EventLoop eventLoop)
{
// We don't want to block under any circumstances, so can't use CHM.computeIfAbsent().
// Instead we accept the slight inefficiency of an unnecessary instantiation of a ConcurrentLinkedDeque.
Deque<PooledConnection> pool = connectionsPerEventLoop.get(eventLoop);
if (pool == null) {
pool = new ConcurrentLinkedDeque<>();
connectionsPerEventLoop.putIfAbsent(eventLoop, pool);
}
return pool;
}
代码示例来源:origin: alibaba/jstorm
public MemoryMapStateBacking(String id) {
this.db = (Map<List<Object>, T>) _dbs.get(id);
if (db == null) {
db = new HashMap<List<Object>, T>();
Map<List<Object>, T> tmp = (Map<List<Object>, T>)
_dbs.putIfAbsent(id, (Map<List<Object>, Object>)db);
if (tmp != null) {
db = tmp;
}
}
}
代码示例来源:origin: alibaba/jstorm
protected ConcurrentLinkedQueue<DRPCRequest> acquireQueue(String function) {
ConcurrentLinkedQueue<DRPCRequest> reqQueue = requestQueues.get(function);
if (reqQueue == null) {
reqQueue = new ConcurrentLinkedQueue<>();
ConcurrentLinkedQueue<DRPCRequest> tmp = requestQueues.putIfAbsent(function, reqQueue);
if (tmp != null) {
reqQueue = tmp;
}
}
return reqQueue;
}
代码示例来源:origin: alibaba/jstorm
public Map<Integer, TkHbCacheTime> getTaskHeartbeatsCache(String topologyId, boolean createIfNotExist) {
Map<Integer, TkHbCacheTime> ret;
ret = taskHeartbeatsCache.get(topologyId);
if (ret == null && createIfNotExist) {
ret = new ConcurrentHashMap<>();
Map<Integer, TkHbCacheTime> tmp = taskHeartbeatsCache.putIfAbsent(topologyId, ret);
if (tmp != null) {
ret = tmp;
}
}
return ret;
}
代码示例来源:origin: apache/storm
private ConcurrentLinkedQueue<OutstandingRequest> getQueue(String function) {
if (function == null) {
throw new IllegalArgumentException("The function for a request cannot be null");
}
ConcurrentLinkedQueue<OutstandingRequest> queue = _queues.get(function);
if (queue == null) {
_queues.putIfAbsent(function, new ConcurrentLinkedQueue<>());
queue = _queues.get(function);
}
return queue;
}
代码示例来源:origin: thinkaurelius/titan
@Override
public Locker getLocker(String lockerName) {
Preconditions.checkNotNull(lockerName);
Locker l = lockers.get(lockerName);
if (null == l) {
l = lockerCreator.apply(lockerName);
final Locker x = lockers.putIfAbsent(lockerName, l);
if (null != x) {
l = x;
}
}
return l;
}
代码示例来源:origin: apache/incubator-dubbo
public static <T> ProviderInvokerWrapper<T> registerProvider(Invoker<T> invoker, URL registryUrl, URL providerUrl) {
ProviderInvokerWrapper<T> wrapperInvoker = new ProviderInvokerWrapper<>(invoker, registryUrl, providerUrl);
String serviceUniqueName = providerUrl.getServiceKey();
ConcurrentMap<Invoker, ProviderInvokerWrapper> invokers = providerInvokers.get(serviceUniqueName);
if (invokers == null) {
providerInvokers.putIfAbsent(serviceUniqueName, new ConcurrentHashMap<>());
invokers = providerInvokers.get(serviceUniqueName);
}
invokers.put(invoker, wrapperInvoker);
return wrapperInvoker;
}
代码示例来源:origin: apache/incubator-dubbo
public static <T> ProviderInvokerWrapper<T> registerProvider(Invoker<T> invoker, URL registryUrl, URL providerUrl) {
ProviderInvokerWrapper<T> wrapperInvoker = new ProviderInvokerWrapper<>(invoker, registryUrl, providerUrl);
String serviceUniqueName = providerUrl.getServiceKey();
ConcurrentMap<Invoker, ProviderInvokerWrapper> invokers = providerInvokers.get(serviceUniqueName);
if (invokers == null) {
providerInvokers.putIfAbsent(serviceUniqueName, new ConcurrentHashMap<>());
invokers = providerInvokers.get(serviceUniqueName);
}
invokers.put(invoker, wrapperInvoker);
return wrapperInvoker;
}
代码示例来源:origin: apache/incubator-shardingsphere
@Override
public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
AtomicInteger count = COUNT_MAP.containsKey(name) ? COUNT_MAP.get(name) : new AtomicInteger(0);
COUNT_MAP.putIfAbsent(name, count);
count.compareAndSet(slaveDataSourceNames.size(), 0);
return slaveDataSourceNames.get(Math.abs(count.getAndIncrement()) % slaveDataSourceNames.size());
}
}
代码示例来源:origin: apache/zookeeper
private AvgMinMaxPercentileCounter getCounterForKey(String key) {
AvgMinMaxPercentileCounter counter = counters.get(key);
if (counter == null) {
counters.putIfAbsent(key, new AvgMinMaxPercentileCounter(key + "_" + name));
counter = counters.get(key);
}
return counter;
}
代码示例来源:origin: apache/zookeeper
private AvgMinMaxCounter getCounterForKey(String key) {
AvgMinMaxCounter counter = counters.get(key);
if (counter == null) {
counters.putIfAbsent(key, new AvgMinMaxCounter(key + "_" + name));
counter = counters.get(key);
}
return counter;
}
代码示例来源:origin: apache/storm
private static LocalServer getLocalServer(String nodeId, int port) {
String key = nodeId + "-" + port;
LocalServer ret = _registry.get(key);
if (ret == null) {
ret = new LocalServer(port);
LocalServer tmp = _registry.putIfAbsent(key, ret);
if (tmp != null) {
ret = tmp;
}
}
return ret;
}
代码示例来源:origin: weibocom/motan
private void addServiceListener(URL url, ServiceListener serviceListener) {
String service = ConsulUtils.getUrlClusterInfo(url);
ConcurrentHashMap<URL, ServiceListener> map = serviceListeners.get(service);
if (map == null) {
serviceListeners.putIfAbsent(service, new ConcurrentHashMap<URL, ServiceListener>());
map = serviceListeners.get(service);
}
synchronized (map) {
map.put(url, serviceListener);
}
}
代码示例来源:origin: weibocom/motan
private void addCommandListener(URL url, CommandListener commandListener) {
String group = url.getGroup();
ConcurrentHashMap<URL, CommandListener> map = commandListeners.get(group);
if (map == null) {
commandListeners.putIfAbsent(group, new ConcurrentHashMap<URL, CommandListener>());
map = commandListeners.get(group);
}
synchronized (map) {
map.put(url, commandListener);
}
}
代码示例来源:origin: thinkaurelius/titan
@Override
public KeyColumnValueStore openDatabase(final String name, StoreMetaData.Container metaData) throws BackendException {
if (!stores.containsKey(name)) {
stores.putIfAbsent(name, new InMemoryKeyColumnValueStore(name));
}
KeyColumnValueStore store = stores.get(name);
Preconditions.checkNotNull(store);
return store;
}
代码示例来源:origin: weibocom/motan
private void addToFailedMap(ConcurrentHashMap<URL, ConcurrentHashSet<NotifyListener>> failedMap, URL url, NotifyListener listener) {
Set<NotifyListener> listeners = failedMap.get(url);
if (listeners == null) {
failedMap.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
listeners = failedMap.get(url);
}
listeners.add(listener);
}
代码示例来源:origin: weibocom/motan
private static StatInfo getServiceStat(String uri) {
StatInfo stat = serviceStat.get(uri);
if (stat == null) {
stat = new StatInfo();
serviceStat.putIfAbsent(uri, stat);
stat = serviceStat.get(uri);
}
return stat;
}
代码示例来源:origin: apache/incubator-dubbo
public static void registerConsumer(Invoker invoker, URL registryUrl, URL consumerUrl, RegistryDirectory registryDirectory) {
ConsumerInvokerWrapper wrapperInvoker = new ConsumerInvokerWrapper(invoker, registryUrl, consumerUrl, registryDirectory);
String serviceUniqueName = consumerUrl.getServiceKey();
Set<ConsumerInvokerWrapper> invokers = consumerInvokers.get(serviceUniqueName);
if (invokers == null) {
consumerInvokers.putIfAbsent(serviceUniqueName, new ConcurrentHashSet<ConsumerInvokerWrapper>());
invokers = consumerInvokers.get(serviceUniqueName);
}
invokers.add(wrapperInvoker);
}
代码示例来源:origin: apache/incubator-dubbo
public static void registerConsumer(Invoker invoker, URL registryUrl, URL consumerUrl, RegistryDirectory registryDirectory) {
ConsumerInvokerWrapper wrapperInvoker = new ConsumerInvokerWrapper(invoker, registryUrl, consumerUrl, registryDirectory);
String serviceUniqueName = consumerUrl.getServiceKey();
Set<ConsumerInvokerWrapper> invokers = consumerInvokers.get(serviceUniqueName);
if (invokers == null) {
consumerInvokers.putIfAbsent(serviceUniqueName, new ConcurrentHashSet<ConsumerInvokerWrapper>());
invokers = consumerInvokers.get(serviceUniqueName);
}
invokers.add(wrapperInvoker);
}
代码示例来源:origin: lets-blade/blade
public static ClassDefine create(Class<?> clazz) {
ClassDefine classDefine = pool.get(clazz);
if (classDefine == null) {
classDefine = new ClassDefine(clazz);
ClassDefine old = pool.putIfAbsent(clazz, classDefine);
if (old != null) {
classDefine = old;
}
}
return classDefine;
}
内容来源于网络,如有侵权,请联系作者删除!