下面列出了java.util.concurrent.ConcurrentHashMap#putAll ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Retrieve a vector that can be sent to another member. This clones all
* of the version information to protect against concurrent modification
* during serialization
*/
public RegionVersionVector<T> getCloneForTransmission() {
Map<T,RegionVersionHolder<T>> liveHolders;
liveHolders = new HashMap<T,RegionVersionHolder<T>>(this.memberToVersion);
ConcurrentHashMap<T, RegionVersionHolder<T>> clonedHolders = new ConcurrentHashMap<T, RegionVersionHolder<T>>(
liveHolders.size(), LOAD_FACTOR, CONCURRENCY_LEVEL);
for (Map.Entry<T, RegionVersionHolder<T>> entry: liveHolders.entrySet()) {
clonedHolders.put(entry.getKey(), entry.getValue().clone());
}
ConcurrentHashMap<T, Long> gcVersions = new ConcurrentHashMap<T, Long>(
this.memberToGCVersion.size(), LOAD_FACTOR, CONCURRENCY_LEVEL);
gcVersions.putAll(this.memberToGCVersion);
RegionVersionHolder<T> clonedLocalHolder;
clonedLocalHolder = this.localExceptions.clone();
//Make sure the holder that we send to the peer does
//have an accurate RegionVersionHolder for our local version
return createCopy(this.myId, clonedHolders, this.localVersion.get(),
gcVersions, this.localGCVersion.get(), false,
clonedLocalHolder);
}
/**
* Retrieve a vector that can be sent to another member. This clones all
* of the version information to protect against concurrent modification
* during serialization
*/
public RegionVersionVector<T> getCloneForTransmission() {
Map<T,RegionVersionHolder<T>> liveHolders;
liveHolders = new HashMap<T,RegionVersionHolder<T>>(this.memberToVersion);
ConcurrentHashMap<T, RegionVersionHolder<T>> clonedHolders = new ConcurrentHashMap<T, RegionVersionHolder<T>>(
liveHolders.size(), LOAD_FACTOR, CONCURRENCY_LEVEL);
for (Map.Entry<T, RegionVersionHolder<T>> entry: liveHolders.entrySet()) {
clonedHolders.put(entry.getKey(), entry.getValue().clone());
}
ConcurrentHashMap<T, Long> gcVersions = new ConcurrentHashMap<T, Long>(
this.memberToGCVersion.size(), LOAD_FACTOR, CONCURRENCY_LEVEL);
gcVersions.putAll(this.memberToGCVersion);
RegionVersionHolder<T> clonedLocalHolder;
clonedLocalHolder = this.localExceptions.clone();
//Make sure the holder that we send to the peer does
//have an accurate RegionVersionHolder for our local version
return createCopy(this.myId, clonedHolders, this.localVersion.get(),
gcVersions, this.localGCVersion.get(), false,
clonedLocalHolder);
}
public void initTaskHb() {
this.taskHbs = new TopologyTaskHbInfo(this.topologyId, this.taskId);
ConcurrentHashMap<Integer, TaskHeartbeat> tmpTaskHbMap = new ConcurrentHashMap<>();
try {
TopologyTaskHbInfo taskHbInfo = zkCluster.topology_heartbeat(topologyId);
if (taskHbInfo != null) {
LOG.info("Found task heartbeat info left in zk for " + topologyId + ": " + taskHbInfo.toString());
if (taskHbInfo.get_taskHbs() != null) {
tmpTaskHbMap.putAll(taskHbInfo.get_taskHbs());
}
}
} catch (Exception e) {
LOG.warn("Failed to get topology heartbeat from zk", e);
}
this.taskHbMap.set(tmpTaskHbMap);
taskHbs.set_taskHbs(tmpTaskHbMap);
}
/**
* putAll adds all key-value pairs from the given map
*/
public void testPutAll() {
ConcurrentHashMap empty = new ConcurrentHashMap();
ConcurrentHashMap map = map5();
empty.putAll(map);
assertEquals(5, empty.size());
assertTrue(empty.containsKey(one));
assertTrue(empty.containsKey(two));
assertTrue(empty.containsKey(three));
assertTrue(empty.containsKey(four));
assertTrue(empty.containsKey(five));
}
/**
* putAll adds all key-value pairs from the given map
*/
public void testPutAll() {
ConcurrentHashMap empty = new ConcurrentHashMap();
ConcurrentHashMap map = map5();
empty.putAll(map);
assertEquals(5, empty.size());
assertTrue(empty.containsKey(one));
assertTrue(empty.containsKey(two));
assertTrue(empty.containsKey(three));
assertTrue(empty.containsKey(four));
assertTrue(empty.containsKey(five));
}
@Override
public Map<String, String> getUserSessionNotes() {
if (entity.getUserSessionNotes() == null) {
return Collections.EMPTY_MAP;
}
ConcurrentHashMap<String, String> copy = new ConcurrentHashMap<>();
copy.putAll(entity.getUserSessionNotes());
return copy;
}
@Override
public Map<K, V> create() {
ConcurrentHashMap<K, V> map = new ConcurrentHashMap<K, V>(Math.max(this.delegate.size(), 16), 0.75f, concurrency);
map.putAll(this.delegate);
return map;
}
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;
}
@Override
public void process(ConcurrentHashMap<K, V> impl) {
impl.putAll(map);
}