Consistent fine-tuning of synchronized and concurrent data structures

In particular, avoiding synchronized Sets and Maps wherever possible (preferring a ConcurrentHashMap even instead of a synchronized Set) and specifying appropriate ConcurrentHashMap initial capacities (even if we end up choosing 16).
This commit is contained in:
Juergen Hoeller
2012-12-12 23:46:26 +01:00
parent a2f8902b3a
commit b9df7d68d9
58 changed files with 380 additions and 387 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,27 +18,26 @@ package org.springframework.mock.jndi;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.naming.NamingException;
import org.springframework.jndi.JndiTemplate;
/**
* Simple extension of the JndiTemplate class that always returns a given
* object. Very useful for testing. Effectively a mock object.
*
* Simple extension of the JndiTemplate class that always returns a given object.
*
* <p>Very useful for testing. Effectively a mock object.
*
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class ExpectedLookupTemplate extends JndiTemplate {
private final Map<String, Object> jndiObjects = new ConcurrentHashMap<String, Object>();
private final Map<String, Object> jndiObjects = new ConcurrentHashMap<String, Object>(16);
/**
* Construct a new JndiTemplate that will always return given objects for
* given names. To be populated through <code>addObject</code> calls.
*
* @see #addObject(String, Object)
*/
public ExpectedLookupTemplate() {
@@ -47,7 +46,6 @@ public class ExpectedLookupTemplate extends JndiTemplate {
/**
* Construct a new JndiTemplate that will always return the given object,
* but honour only requests for the given name.
*
* @param name the name the client is expected to look up
* @param object the object that will be returned
*/
@@ -56,9 +54,7 @@ public class ExpectedLookupTemplate extends JndiTemplate {
}
/**
* Add the given object to the list of JNDI objects that this template will
* expose.
*
* Add the given object to the list of JNDI objects that this template will expose.
* @param name the name the client is expected to look up
* @param object the object that will be returned
*/

View File

@@ -44,7 +44,8 @@ class ContextCache {
/**
* Map of context keys to Spring ApplicationContext instances.
*/
private final Map<MergedContextConfiguration, ApplicationContext> contextMap = new ConcurrentHashMap<MergedContextConfiguration, ApplicationContext>();
private final Map<MergedContextConfiguration, ApplicationContext> contextMap =
new ConcurrentHashMap<MergedContextConfiguration, ApplicationContext>(64);
private int hitCount;
@@ -69,7 +70,6 @@ class ContextCache {
/**
* Return whether there is a cached context for the given key.
*
* @param key the context key (never <code>null</code>)
*/
boolean contains(MergedContextConfiguration key) {
@@ -79,10 +79,8 @@ class ContextCache {
/**
* Obtain a cached ApplicationContext for the given key.
*
* <p>The {@link #getHitCount() hit} and {@link #getMissCount() miss}
* counts will be updated accordingly.
*
* @param key the context key (never <code>null</code>)
* @return the corresponding ApplicationContext instance,
* or <code>null</code> if not found in the cache.
@@ -135,7 +133,6 @@ class ContextCache {
/**
* Explicitly add an ApplicationContext instance to the cache under the given key.
*
* @param key the context key (never <code>null</code>)
* @param context the ApplicationContext instance (never <code>null</code>)
*/
@@ -147,7 +144,6 @@ class ContextCache {
/**
* Remove the context with the given key.
*
* @param key the context key (never <code>null</code>)
* @return the corresponding ApplicationContext instance, or <code>null</code>
* if not found in the cache.
@@ -162,11 +158,9 @@ class ContextCache {
* {@link #remove removing} the context from the cache and explicitly
* {@link ConfigurableApplicationContext#close() closing} it if it is an
* instance of {@link ConfigurableApplicationContext}.
*
* <p>Generally speaking, you would only call this method if you change the
* state of a singleton bean, potentially affecting future interaction with
* the context.
*
* @param key the context key (never <code>null</code>)
* @see #remove
*/
@@ -192,11 +186,8 @@ class ContextCache {
* as the {@link #hitCount hit} and {@link #missCount miss} counts.
*/
public String toString() {
return new ToStringCreator(this)//
.append("size", size())//
.append("hitCount", getHitCount())//
.append("missCount", getMissCount())//
.toString();
return new ToStringCreator(this).append("size", size()).append("hitCount", getHitCount()).
append("missCount", getMissCount()).toString();
}
}

View File

@@ -21,12 +21,13 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
@@ -111,7 +112,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
protected final TransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource();
private final Map<Method, TransactionContext> transactionContextCache =
Collections.synchronizedMap(new IdentityHashMap<Method, TransactionContext>());
new ConcurrentHashMap<Method, TransactionContext>(8);
private TransactionConfigurationAttributes configurationAttributes;