Return-Path: Delivered-To: apmail-db-derby-dev-archive@www.apache.org Received: (qmail 26793 invoked from network); 21 Dec 2006 09:35:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Dec 2006 09:35:32 -0000 Received: (qmail 76022 invoked by uid 500); 21 Dec 2006 09:35:27 -0000 Delivered-To: apmail-db-derby-dev-archive@db.apache.org Received: (qmail 75944 invoked by uid 500); 21 Dec 2006 09:35:26 -0000 Mailing-List: contact derby-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: Delivered-To: mailing list derby-dev@db.apache.org Received: (qmail 75919 invoked by uid 99); 21 Dec 2006 09:35:26 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Dec 2006 01:35:26 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=UNPARSEABLE_RELAY X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: local policy) Received: from [192.18.1.36] (HELO gmp-ea-fw-1.sun.com) (192.18.1.36) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Dec 2006 01:35:14 -0800 Received: from d1-emea-10.sun.com ([192.18.2.120]) by gmp-ea-fw-1.sun.com (8.13.6+Sun/8.12.9) with ESMTP id kBL9YrCp007190 for ; Thu, 21 Dec 2006 09:34:53 GMT Received: from conversion-daemon.d1-emea-10.sun.com by d1-emea-10.sun.com (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) id <0JAM00A01ALEG100@d1-emea-10.sun.com> (original mail from Knut.Hatlen@Sun.COM) for derby-dev@db.apache.org; Thu, 21 Dec 2006 09:34:53 +0000 (GMT) Received: from localhost ([129.159.112.231]) by d1-emea-10.sun.com (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) with ESMTPSA id <0JAM00JX3AM4CXA7@d1-emea-10.sun.com> for derby-dev@db.apache.org; Thu, 21 Dec 2006 09:34:53 +0000 (GMT) Date: Thu, 21 Dec 2006 10:34:51 +0100 From: Knut Anders Hatlen Subject: Re: A question about some synchronized code In-reply-to: Sender: Knut.Hatlen@Sun.COM To: derby-dev@db.apache.org Message-id: Organization: Sun Microsystems MIME-version: 1.0 Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT References: User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.91 (usg-unix-v) X-Virus-Checked: Checked by ClamAV on apache.org Raymond Raymond writes: > Today, when I read the source code of > org.apache.derby.impl.store.raw.data.CachedPage.java, > I found some synchronized code like: > > public boolean isDirty() { > synchronized (this) { > return isDirty || preDirty; > } > } > > public boolean isActuallyDirty() { > synchronized (this) { > return isDirty; > } > } > > I have two questions about them, hope someone can answer me: > 1. Is it necessary to synchronize the code in these places? There must be some kind of synchronization since the isDirty and preDirty flags can be accessed and modified by multiple threads, but it could probably be done on a higher level. Many of the callers of these methods are already synchronized on the page object, and in those cases the synchronization is not needed. There does however seem to be callers that don't synchronize too. Note that the javadoc for Cacheable.isDirty() says "MT - thread safe". Removing the synchronization in CachedPage.isDirty() would break that contract. > 2. I saw someone else commented on some code (in derby) said it's > not good to return from inside of a synchronized block. What > potential problem it will cause if return from inside of a > synchronized block? I'm not sure what problems that could be. When returning from a synchronized block, the monitor is released (unless the thread already owned the monitor when it entered the synchronized block). Do you have a pointer to that discussion? -- Knut Anders