本文整理汇总了Java中java.util.concurrent.ConcurrentHashMap.size方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentHashMap.size方法的具体用法?Java ConcurrentHashMap.size怎么用?Java ConcurrentHashMap.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ConcurrentHashMap
的用法示例。
在下文中一共展示了ConcurrentHashMap.size方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getQueryStats
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
protected QueryStats getQueryStats(String sql) {
if (sql==null) sql = "";
ConcurrentHashMap<String,QueryStats> queries = SlowQueryReport.this.queries;
if (queries==null) {
if (log.isWarnEnabled()) log.warn("Connection has already been closed or abandoned");
return null;
}
QueryStats qs = queries.get(sql);
if (qs == null) {
qs = new QueryStats(sql);
if (queries.putIfAbsent(sql,qs)!=null) {
qs = queries.get(sql);
} else {
//we added a new element, see if we need to remove the oldest
if (queries.size() > maxQueries) {
removeOldest(queries);
}
}
}
return qs;
}
示例2: writeTransform
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
@Override
public Object writeTransform(Field f, Class<?> clazz, Object originalValue) {
if (f.getType().equals(ConcurrentHashMap.class)) {
Object[] result = null;
if (originalValue != null) {
ConcurrentHashMap<?, ?> m = (ConcurrentHashMap<?, ?>) originalValue;
result = new Object[m.size() * 2];
int i = 0;
for (Map.Entry<?, ?> e : m.entrySet()) {
result[i++] = e.getKey();
result[i++] = e.getValue();
}
}
return result;
} else {
return super.writeTransform(f, clazz, originalValue);
}
}
示例3: size
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
public int size() {
if (k1_k2V_map.size() == 0) return 0;
int result = 0;
for (ConcurrentHashMap<K2, V> k2V_map : k1_k2V_map.values()) {
result += k2V_map.size();
}
return result;
}
示例4: removeOldest
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* Sort QueryStats by last invocation time
* @param queries
*/
protected void removeOldest(ConcurrentHashMap<String,QueryStats> queries) {
ArrayList<QueryStats> list = new ArrayList<QueryStats>(queries.values());
Collections.sort(list, queryStatsComparator);
int removeIndex = 0;
while (queries.size() > maxQueries) {
String sql = list.get(removeIndex).getQuery();
queries.remove(sql);
if (log.isDebugEnabled()) log.debug("Removing slow query, capacity reached:"+sql);
removeIndex++;
}
}
示例5: HeartBeatTask
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
public HeartBeatTask(Client client, ConcurrentHashMap<Peer,Integer> peerList, int hbPort, int swPort) {
this.peerList = peerList;
this.hbPort = hbPort;
this.swPort = swPort;
this.size =peerList.size();
HeartBeatTask.client = client;
}
示例6: sayHelloWithHashMap
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
@Override
public String sayHelloWithHashMap(ConcurrentHashMap<String, String> receivedHashMap)
throws RemoteException {
int hashMapSize = 0;
hashMapSize = receivedHashMap.size();
String response = "Hello with hashMapSize == " + hashMapSize;
return response;
}
示例7: testWeakKey
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* Example using a Cleaner to remove WeakKey references from a Map.
*/
@Test
void testWeakKey() {
ConcurrentHashMap<WeakKey<String>, String> map = new ConcurrentHashMap<>();
Cleaner cleaner = Cleaner.create();
String key = new String("foo"); // ensure it is not interned
String data = "bar";
map.put(new WeakKey<>(key, cleaner, map), data);
WeakKey<String> k2 = new WeakKey<>(key, cleaner, map);
Assert.assertEquals(map.get(k2), data, "value should be found in the map");
key = null;
System.gc();
Assert.assertNotEquals(map.get(k2), data, "value should not be found in the map");
final long CYCLE_MAX = Utils.adjustTimeout(30L);
for (int i = 1; map.size() > 0 && i < CYCLE_MAX; i++) {
map.forEach( (k, v) -> System.out.printf(" k: %s, v: %s%n", k, v));
try {
Thread.sleep(10L);
} catch (InterruptedException ie) {}
}
Assert.assertEquals(map.size(), 0, "Expected map to be empty;");
cleaner = null;
}
示例8: removeTargetIfRequired
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* Remove the target.
*
* @param subscribedMethods the subscribed methods.
* @param mTargetMap the target map.
* @param targetObject the target object.
*/
private void removeTargetIfRequired(ConcurrentHashMap<String, SubscriberHolder> subscribedMethods,
ConcurrentHashMap<Object,
ConcurrentHashMap<String, SubscriberHolder>> mTargetMap,
Object targetObject) {
if (subscribedMethods.size() == 0) {
mTargetMap.remove(targetObject);
}
}
示例9: removeEventIfRequired
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* Remove the event.
*
* @param mTargetMap the target map.
* @param mEventsToTargetsMapEntry the event to target map entry.
*/
private void removeEventIfRequired(ConcurrentHashMap<Object,
ConcurrentHashMap<String, SubscriberHolder>> mTargetMap,
Map.Entry<Class<?>, ConcurrentHashMap<Object,
ConcurrentHashMap<String,
SubscriberHolder>>> mEventsToTargetsMapEntry) {
if (mTargetMap.size() == 0) {
mEventsToTargetsMap.remove(mEventsToTargetsMapEntry.getKey());
}
}
示例10: manageCache
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
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;
}
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:45,代码来源:ReferenceCountingReadOnlyIndexReaderFactory.java
示例11: main
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
public static void main(String[] args) {
ConcurrentHashMap map = new ConcurrentHashMap();
map.put(1,2);
map.get(1);
map.size();
}
示例12: initClusterCenters
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/** Initialize cluster centers. */
private Vector[] initClusterCenters(SparseDistributedMatrix points, int k) {
// Initialize empty centers and point costs.
int ptsCnt = points.rowSize();
String cacheName = ((SparseDistributedMatrixStorage)points.getStorage()).cacheName();
// Initialize the first center to a random point.
Vector sample = localCopyOf(points.viewRow(rnd.nextInt(ptsCnt)));
List<Vector> centers = new ArrayList<>();
List<Vector> newCenters = new ArrayList<>();
newCenters.add(sample);
centers.add(sample);
final ConcurrentHashMap<Integer, Double> costs = new ConcurrentHashMap<>();
// On each step, sample 2 * k points on average with probability proportional
// to their squared distance from the centers. Note that only distances between points
// and new centers are computed in each iteration.
int step = 0;
UUID uid = points.getUUID();
while (step < initSteps) {
// We assume here that costs can fit into memory of one node.
ConcurrentHashMap<Integer, Double> newCosts = getNewCosts(points, newCenters, cacheName);
// Merge costs with new costs.
for (Integer ind : newCosts.keySet())
costs.merge(ind, newCosts.get(ind), Math::min);
double sumCosts = costs.values().stream().mapToDouble(Double::valueOf).sum();
newCenters = getNewCenters(k, costs, uid, sumCosts, cacheName);
centers.addAll(newCenters);
step++;
}
List<Vector> distinctCenters = centers.stream().distinct().collect(Collectors.toList());
if (distinctCenters.size() <= k)
return distinctCenters.toArray(new Vector[] {});
else {
// Finally, we might have a set of more than k distinct candidate centers; weight each
// candidate by the number of points in the dataset mapping to it and run a local k-means++
// on the weighted centers to pick k of them
ConcurrentHashMap<Integer, Integer> centerInd2Weight = weightCenters(uid, distinctCenters, cacheName);
List<Double> weights = new ArrayList<>(centerInd2Weight.size());
for (int i = 0; i < distinctCenters.size(); i++)
weights.add(i, Double.valueOf(centerInd2Weight.getOrDefault(i, 0)));
DenseLocalOnHeapMatrix dCenters = MatrixUtil.fromList(distinctCenters, true);
return new KMeansLocalClusterer(getDistanceMeasure(), 30, seed).cluster(dCenters, k, weights).centers();
}
}