[ 
https://issues.apache.org/jira/browse/OGNL-20?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13097809#comment-13097809
 ] 

Julien Aymé edited comment on OGNL-20 at 9/6/11 8:50 AM:
---------------------------------------------------------

Note that you also could use ConcurrentHashMap instead of HashMap. Also, since 
in many place it is not harmful if the cached object is evaluated twice, you 
can remove the whole synchronized block:
{code}
// When initializing the cache:
cache = new ConcurrentHashMap();

// Looking up into the cache
Object cachedObject = cache.get(key);
if (null == cachedObject) {
    // No need to use a synchronized block here if we don't care that the 
getRealObject method is invoked more than once.
    Object realObject = getRealObject(key);
    cache.put(key, realObject);
    cachedObject = realObject;
}
return cachedObject;
{code}

As shown above, the use of a ConcurrentHashMap allows to discard any 
synchronized block, and any custom lock use.

HTH,
Regards,
Julien

      was (Author: julien.a...@gmail.com):
    Note that you also could use ConcurrentHashMap instead of HashMap. Also, 
since in many place it is not harmful if the cached object is evaluated twice, 
you can remove the whole synchronized block:
<code>
cache = new ConcurrentHashMap();

Object cachedObject = cache.get(key);
if (null == cachedObject) {

<code>

  
> Performance - Replace synchronized blocks with ReentrantReadWriteLock
> ---------------------------------------------------------------------
>
>                 Key: OGNL-20
>                 URL: https://issues.apache.org/jira/browse/OGNL-20
>             Project: OGNL
>          Issue Type: Improvement
>         Environment: ALL
>            Reporter: Greg Lively
>
> I've noticed a lot of synchronized blocks of code in OGNL. For the most part, 
> these synchronized blocks are controlling access to HashMaps, etc. I believe 
> this could be done far better using ReentrantReadWriteLocks. 
> ReentrantReadWriteLock allows unlimited concurrent access, and single threads 
> only for writes. Perfect in an environment where the ratio of reads  is far 
> higher than writes; which is typically the scenario for caching. Plus the 
> access control can be tuned for reads and writes; not just a big 
> synchronized{} wrapping a bunch of code.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira


Reply via email to