java.util.Collections.synchronizedMap()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中java.util.Collections.synchronizedMap()方法的一些代码示例,展示了Collections.synchronizedMap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collections.synchronizedMap()方法的具体详情如下:
包路径:java.util.Collections
类名称:Collections
方法名:synchronizedMap

Collections.synchronizedMap介绍

[英]Returns a wrapper on the specified map which synchronizes all access to the map.
[中]

代码示例

代码示例来源:origin: ch.qos.logback/logback-classic

public ContextJNDISelector(LoggerContext context) {
  synchronizedContextMap = Collections.synchronizedMap(new HashMap<String, LoggerContext>());
  defaultContext = context;
}

代码示例来源:origin: ch.qos.logback/logback-classic

private Map<String, String> duplicateAndInsertNewMap(Map<String, String> oldMap) {
  Map<String, String> newMap = Collections.synchronizedMap(new HashMap<String, String>());
  if (oldMap != null) {
    // we don't want the parent thread modifying oldMap while we are
    // iterating over it
    synchronized (oldMap) {
      newMap.putAll(oldMap);
    }
  }
  copyOnThreadLocal.set(newMap);
  return newMap;
}

代码示例来源:origin: facebook/litho

public T customAttribute(String key, Object value) {
 if (mCustomAttributes == null) {
  mCustomAttributes = Collections.synchronizedMap(new HashMap<String, Object>());
 }
 mCustomAttributes.put(key, value);
 return (T) this;
}

代码示例来源:origin: kiegroup/jbpm

protected List<TaskSummary> collectTasksByPotentialOwners(List<Object[]> tasksByGroups) {
  Set<Long> tasksIds = Collections.synchronizedSet(new HashSet<Long>());
  Map<Long, List<String>> potentialOwners = Collections.synchronizedMap(new HashMap<Long, List<String>>());
  for (Object o : tasksByGroups) {
    Object[] get = (Object[]) o;
    tasksIds.add((Long) get[0]);
    if (potentialOwners.get((Long) get[0]) == null) {
      potentialOwners.put((Long) get[0], new ArrayList<String>());
    }
    potentialOwners.get((Long) get[0]).add((String) get[1]);
  }
  if (!tasksIds.isEmpty()) {
    List<TaskSummary> tasks = (List<TaskSummary>)persistenceContext.queryWithParametersInTransaction("TaskSummariesByIds", 
        persistenceContext.addParametersToMap("taskIds", tasksIds),
        ClassUtil.<List<TaskSummary>>castClass(List.class));
        
    for (TaskSummary ts : tasks) {
      ((InternalTaskSummary) ts).setPotentialOwners(potentialOwners.get(ts.getId()));
    }
    return tasks;
  }
  return new ArrayList<TaskSummary>();
}

代码示例来源:origin: stackoverflow.com

// Create cache
final int MAX_ENTRIES = 100;
Map cache = new LinkedHashMap(MAX_ENTRIES+1, .75F, true) {
  // This method is called just after a new entry has been added
  public boolean removeEldestEntry(Map.Entry eldest) {
    return size() > MAX_ENTRIES;
  }
};

// Add to cache
Object key = "key";
cache.put(key, object);

// Get object
Object o = cache.get(key);
if (o == null && !cache.containsKey(key)) {
  // Object not in cache. If null is not a possible value in the cache,
  // the call to cache.contains(key) is not needed
}

// If the cache is to be used by multiple threads,
// the cache must be wrapped with code to synchronize the methods
cache = (Map)Collections.synchronizedMap(cache);

代码示例来源:origin: apache/ignite

super.beforeTestsStarted();
else {
  cacheCfgMap = Collections.synchronizedMap(new HashMap<String, CacheConfiguration[]>());
      Ignite ignite = startGrid(i);
      CacheConfiguration[] cacheCfgs = cacheCfgMap.get(ignite.name());

代码示例来源:origin: apache/hive

Collections.synchronizedMap(new LinkedHashMap<>());
 PartitionDetails details = new PartitionDetails();
 details.fullSpec = fullPartSpec;
 partitionDetailsMap.put(partPath, details);
for (Future<Partition> future : futures) {
 Partition partition = future.get();
 result.put(partition.getSpec(), partition);

代码示例来源:origin: apache/metron

private Map<String, Statusable<PAGE_T>> getUserJobs(String username) {
 return jobs.getOrDefault(username, Collections.synchronizedMap(new HashMap<>()));
}

代码示例来源:origin: facebook/litho

@Override
public void addDebugInfo(String key, Object value) {
 if (mDebugInfo == null) {
  mDebugInfo = Collections.synchronizedMap(new HashMap<String, Object>());
 }
 mDebugInfo.put(key, value);
}

代码示例来源:origin: kiegroup/jbpm

public List<TaskSummary> getTasksAssignedByGroups(List<String> groupIds) {
  if(groupIds == null || groupIds.isEmpty()){
   return Collections.EMPTY_LIST;
  }
  List<Object[]> tasksByGroups = (List<Object[]>) persistenceContext.queryWithParametersInTransaction("TasksAssignedAsPotentialOwnerByGroups", 
      persistenceContext.addParametersToMap("groupIds", groupIds),
      ClassUtil.<List<Object[]>>castClass(List.class));
      
  Set<Long> tasksIds = Collections.synchronizedSet(new HashSet<Long>());
  Map<Long, List<String>> potentialOwners = Collections.synchronizedMap(new HashMap<Long, List<String>>());
  for (Object o : tasksByGroups) {
    Object[] get = (Object[]) o;
    tasksIds.add((Long) get[0]);
    if (potentialOwners.get((Long) get[0]) == null) {
      potentialOwners.put((Long) get[0], new ArrayList<String>());
    }
    potentialOwners.get((Long) get[0]).add((String) get[1]);
  }
  if (!tasksIds.isEmpty()) {
    List<TaskSummary> tasks = (List<TaskSummary>) persistenceContext.queryWithParametersInTransaction("TaskSummariesByIds", 
          persistenceContext.addParametersToMap("taskIds", tasksIds),
          ClassUtil.<List<TaskSummary>>castClass(List.class));
    for (TaskSummary ts : tasks) {
      ((InternalTaskSummary) ts).setPotentialOwners(potentialOwners.get(ts.getId()));
    }
    return tasks;
  }
  return new ArrayList<TaskSummary>();
}

代码示例来源:origin: redisson/redisson

public InMemoryQueuePersistor() {
  this.objects = Collections.synchronizedMap(new HashMap<Long, T>());
}

代码示例来源:origin: apache/flink

map = CACHE_CLASSES.get(classLoader);
 if (map == null) {
  map = Collections.synchronizedMap(
   new WeakHashMap<String, WeakReference<Class<?>>>());
  CACHE_CLASSES.put(classLoader, map);
WeakReference<Class<?>> ref = map.get(name);
if (ref != null) {
  clazz = ref.get();
 } catch (ClassNotFoundException e) {
  map.put(name, new WeakReference<Class<?>>(NEGATIVE_CACHE_SENTINEL));
  return null;
 map.put(name, new WeakReference<Class<?>>(clazz));
 return clazz;
} else if (clazz == NEGATIVE_CACHE_SENTINEL) {

代码示例来源:origin: Impetus/Kundera

Map<?, Map<String, String>> clientProperties = new HashMap<>();
    .synchronizedMap(new HashMap<String, Map<String, String>>());
    if (entry.getValue().get("kundera.pu") != null)
      puNames.append(entry.getValue().get("kundera.pu"));
      puNames.append(",");
      emf = Persistence.createEntityManagerFactory("testPU", entityConfigurations.get(clazzName));

代码示例来源:origin: ch.qos.logback/logback-classic

public void setContextMap(Map<String, String> contextMap) {
    lastOperation.set(WRITE_OPERATION);

    Map<String, String> newMap = Collections.synchronizedMap(new HashMap<String, String>());
    newMap.putAll(contextMap);

    // the newMap replaces the old one for serialisation's sake
    copyOnThreadLocal.set(newMap);
  }
}

代码示例来源:origin: voldemort/voldemort

public void testAsyncOperationCache() throws Exception {
  Map<Integer, AsyncOperation> operations = Collections.synchronizedMap(new AsyncOperationCache(2));
  final CountDownLatch countDownLatch = new CountDownLatch(1);
  AsyncOperation completeLater = new AsyncOperation(0, "test") {
  operations.put(0, completeLater);
  operations.put(1, completeNow);
  operations.put(2, completeSoon);

代码示例来源:origin: com.thoughtworks.xstream/xstream

/**
 * Registers the mapping of the Java class name to the QName
 */
public synchronized void registerMapping(QName qname, String javaClassName) {
  if (javaToQName == null) {
    javaToQName = Collections.synchronizedMap(new HashMap());
  }
  if (qnameToJava == null) {
    qnameToJava = Collections.synchronizedMap(new HashMap());
  }
  javaToQName.put(javaClassName, qname);
  qnameToJava.put(qname, javaClassName);
}

代码示例来源:origin: ethereum/ethereumj

validatorMap = Collections.synchronizedMap(new HashMap<Long, BlockHeaderValidator>());
List<Pair<Long, BlockHeaderValidator>> validators = config.getBlockchainConfig().
    getConfigForBlock(blockNumber).headerValidators();
for (Pair<Long, BlockHeaderValidator> validator : validators) {
  if (validator.getLeft() <= getBestKnownBlock().getNumber()) {
    validatorMap.put(validator.getLeft(), validator.getRight());
BlockHeaderValidator validator = validatorMap.get(blockNumber);
if (validator != null) {
  BlockHeaderRule.ValidationResult result = validator.validate(blockHeader);

代码示例来源:origin: Activiti/Activiti

/** Cache with no limit */
public ProcessDefinitionInfoCache(CommandExecutor commandExecutor) {
 this.commandExecutor = commandExecutor;
 this.cache = Collections.synchronizedMap(new HashMap<String, ProcessDefinitionInfoCacheObject>());
}

代码示例来源:origin: apache/flink

map = CACHE_CLASSES.get(classLoader);
  if (map == null) {
    map = Collections.synchronizedMap(
        new WeakHashMap<String, WeakReference<Class<?>>>());
    CACHE_CLASSES.put(classLoader, map);
WeakReference<Class<?>> ref = map.get(name);
if (ref != null) {
  clazz = ref.get();
  } catch (ClassNotFoundException e) {
    map.put(name, new WeakReference<Class<?>>(NEGATIVE_CACHE_SENTINEL));
    return null;
  map.put(name, new WeakReference<Class<?>>(clazz));
  return clazz;
} else if (clazz == NEGATIVE_CACHE_SENTINEL) {

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * Substitutes variables in <code>aString</code> with the environment values in the system properties
 *
 * @param aString
 *          the string on which to apply the substitution.
 * @param systemProperties
 *          the system properties to use
 * @return the string with the substitution applied.
 */
public static final synchronized String environmentSubstitute( String aString,
 Map<String, String> systemProperties ) {
 Map<String, String> sysMap = new HashMap<String, String>();
 synchronized ( sysMap ) {
  sysMap.putAll( Collections.synchronizedMap( systemProperties ) );
  aString = substituteWindows( aString, sysMap );
  aString = substituteUnix( aString, sysMap );
  aString = substituteHex( aString );
  return aString;
 }
}

相关文章

微信公众号

最新文章

更多

Collections类方法