Return-Path: Delivered-To: apmail-struts-dev-archive@www.apache.org Received: (qmail 92638 invoked from network); 24 Jul 2009 19:46:44 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 24 Jul 2009 19:46:44 -0000 Received: (qmail 74713 invoked by uid 500); 24 Jul 2009 19:47:49 -0000 Delivered-To: apmail-struts-dev-archive@struts.apache.org Received: (qmail 74626 invoked by uid 500); 24 Jul 2009 19:47:48 -0000 Mailing-List: contact dev-help@struts.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Struts Developers List" Reply-To: "Struts Developers List" Delivered-To: mailing list dev@struts.apache.org Received: (qmail 74616 invoked by uid 99); 24 Jul 2009 19:47:48 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 Jul 2009 19:47:48 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of musachy@gmail.com designates 209.85.221.201 as permitted sender) Received: from [209.85.221.201] (HELO mail-qy0-f201.google.com) (209.85.221.201) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 Jul 2009 19:47:37 +0000 Received: by qyk39 with SMTP id 39so2506864qyk.23 for ; Fri, 24 Jul 2009 12:47:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=A0CMzi3FVVJHIXB5mA2+BmE3RvuxPUE6NbbwPWsXhZo=; b=b3FImVg4newYkT/MYN5aJ2dh0QqfDbuowAvkc3F0bJ8pTL26+KWMUzPo2zmZcaCugR t7ySakOurTW/jei8CvTh38b/xtrH9npsnhQ0rcCtCuzhHCJuSsQAVWbOqvrmA95Si8u/ i1zpQ99Qo94o/iE4ZxsnifQ0rcMcKFuBzV+vY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=RPl4RjXik6pijVh3nTz+3uSIEn4FLO6/gPHcc6Vr4qg159STW7bDkePoTl3/krgDNk +yDK8kHEpCT0/ZYFWbGFSt/w+gHGOrmmNUxhenVA8n4aROsUbYKmHr6eOSUAzuCL+hqY hG3m15nNCdC8Miiqd5ppDwKcYQMOcuJ2SkQwU= MIME-Version: 1.0 Received: by 10.220.97.77 with SMTP id k13mr2768697vcn.30.1248464836473; Fri, 24 Jul 2009 12:47:16 -0700 (PDT) In-Reply-To: References: <4A69FB03.2020209@it-neering.net> Date: Fri, 24 Jul 2009 12:47:16 -0700 Message-ID: Subject: Re: avoiding synchronized in AnnotationActionValidatorManager From: Musachy Barroso To: Struts Developers List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Patch here(this one works), because I am haven't finished my PhD in Math yet, so I am having a hard time working with Crucible: Index: core/src/main/java/com/opensymphony/xwork2/validator/AnnotationActionValidatorManager.java =================================================================== --- core/src/main/java/com/opensymphony/xwork2/validator/AnnotationActionValidatorManager.java (revision 1973) +++ core/src/main/java/com/opensymphony/xwork2/validator/AnnotationActionValidatorManager.java Fri Jul 24 12:16:49 PDT 2009 @@ -16,6 +16,8 @@ import java.io.IOException; import java.io.InputStream; import java.util.*; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; /** * AnnotationActionValidatorManager is the entry point into XWork's annotations-based validator framework. @@ -31,13 +33,15 @@ */ protected static final String VALIDATION_CONFIG_SUFFIX = "-validation.xml"; - private final Map> validatorCache = Collections.synchronizedMap(new HashMap>()); - private final Map> validatorFileCache = Collections.synchronizedMap(new HashMap>()); + private final Map> validatorCache = new HashMap>(); + private final Map> validatorFileCache = new HashMap>(); private static final Logger LOG = LoggerFactory.getLogger(AnnotationActionValidatorManager.class); private ValidatorFactory validatorFactory; private ValidatorFileParser validatorFileParser; + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + @Inject public void setValidatorFactory(ValidatorFactory fac) { this.validatorFactory = fac; @@ -48,20 +52,46 @@ this.validatorFileParser = parser; } - public synchronized List getValidators(Class clazz, String context) { + public List getValidators(Class clazz, String context) { return getValidators(clazz, context, null); } - public synchronized List getValidators(Class clazz, String context, String method) { + public List getValidators(Class clazz, String context, String method) { final String validatorKey = buildValidatorKey(clazz, context); + //because the write lock is inside the read lock, we need to double check the + //preconditions inside the write lock, as multiple threads could enter the write lock + //sequentially, so if FileManager.isReloadingConfigs() is true (devMode) the validator configs + //could potentially be built multiple times when the cache is cold, this is not a problem for development + //and it saves us from having to synchronize this method which would affect performance + lock.readLock().lock(); + try { - if (validatorCache.containsKey(validatorKey)) { - if (FileManager.isReloadingConfigs()) { + if (validatorCache.containsKey(validatorKey)) { + if (FileManager.isReloadingConfigs()) { + lock.readLock().unlock(); + lock.writeLock().lock(); + try { + if (validatorCache.containsKey(validatorKey)) - validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, true, null)); + validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, true, null)); + } finally { + lock.readLock().lock(); + lock.writeLock().unlock(); - } + } + } - } else { + } else { + lock.readLock().unlock(); + lock.writeLock().lock(); + try { + if (!validatorCache.containsKey(validatorKey)) - validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, false, null)); + validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, false, null)); + } finally { + lock.readLock().lock(); + lock.writeLock().unlock(); - } + } + } + } finally { + lock.readLock().unlock(); + } // get the set of validator configs List cfgs = validatorCache.get(validatorKey); On Fri, Jul 24, 2009 at 11:38 AM, Wes Wannemacher wrote: > I had to go through the registration process a while ago for another > code review Musachy posted. To be honest though, I don't know if you > have a my.atlassian.com account if it would work... > > -Wes > > On Fri, Jul 24, 2009 at 2:18 PM, Rene Gielen wrote: >> Hmm, how can I authenticate to Fisheye and Crucible? >> >> Musachy Barroso schrieb: >>> >>> nothing like sending an email to find the answer. That whole thing is >>> broken. >>> >>> musachy >>> >>> On Fri, Jul 24, 2009 at 10:32 AM, Musachy Barroso >>> wrote: >>>> >>>> I changed the synchronized implementation of >>>> AnnotationActionValidatorManager.getValidators to use java concurrent >>>> API, I could use some code review on this one: >>>> >>>> http://fisheye6.atlassian.com/cru/DEMO-32 >>>> >>>> musachy >>>> >>>> -- >>>> "Hey you! Would you help me to carry the stone?" Pink Floyd >>>> >>> >>> >>> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org >> For additional commands, e-mail: dev-help@struts.apache.org >> >> > > > > -- > Wes Wannemacher > > Head Engineer, WanTii, Inc. > Need Training? Struts, Spring, Maven, Tomcat... > Ask me for a quote! > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org > For additional commands, e-mail: dev-help@struts.apache.org > > -- "Hey you! Would you help me to carry the stone?" Pink Floyd --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org For additional commands, e-mail: dev-help@struts.apache.org