Return-Path: Delivered-To: apmail-incubator-river-dev-archive@minotaur.apache.org Received: (qmail 454 invoked from network); 28 Nov 2010 23:17:45 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 28 Nov 2010 23:17:45 -0000 Received: (qmail 66647 invoked by uid 500); 28 Nov 2010 23:17:45 -0000 Delivered-To: apmail-incubator-river-dev-archive@incubator.apache.org Received: (qmail 66621 invoked by uid 500); 28 Nov 2010 23:17:45 -0000 Mailing-List: contact river-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: river-dev@incubator.apache.org Delivered-To: mailing list river-dev@incubator.apache.org Received: (qmail 66613 invoked by uid 99); 28 Nov 2010 23:17:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Nov 2010 23:17:45 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of pats@acm.org designates 209.86.89.70 as permitted sender) Received: from [209.86.89.70] (HELO elasmtp-banded.atl.sa.earthlink.net) (209.86.89.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Nov 2010 23:17:37 +0000 Received: from [70.230.203.59] (helo=[192.168.1.104]) by elasmtp-banded.atl.sa.earthlink.net with esmtpa (Exim 4.67) (envelope-from ) id 1PMqUQ-0000lL-1c for river-dev@incubator.apache.org; Sun, 28 Nov 2010 18:17:02 -0500 Message-ID: <4CF2E2D2.8060803@acm.org> Date: Sun, 28 Nov 2010 15:16:34 -0800 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.11) Gecko/20101013 Thunderbird/3.1.6 MIME-Version: 1.0 To: river-dev@incubator.apache.org Subject: Re: Possible multi-threading bug References: <4CF2A8F2.9030701@acm.org> <4CF2DD34.70506@zeus.net.au> In-Reply-To: <4CF2DD34.70506@zeus.net.au> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-ELNK-Trace: 9a090983a806273c061ba25959e76cc985338a7d01cb3b6a7e972de0d01da940a0ae98c44dd43b7b6ec6199cc0b0d075350badd9bab72f9c350badd9bab72f9c X-Originating-IP: 70.230.203.59 In the way in which this is used, I expect most of the calls to be to increment. It has to be synchronized, so it seems simplest to synchronize all the methods. I've done some more experiments, and decided this is a real problem. As part of my debug effort, I increased the number of threads in the stress test, so that it fails much more often. I also added some debug printouts, one of which was showing up in conjunction with some but not all failures, so I thought it was irrelevant. With the additional synchronization, the debug message shows up in all failures. I think I actually had two forms of failure, one of which is fixed by the synchronization. In the failure case that has been fixed, everything works, no debug messages, but the test never admits to having finished, exactly the symptom I would expect from this issue. I plan to check in the enhanced test as well as the fixes, because it only takes a few minutes longer than the current size, and is much better at finding bugs. Patricia On 11/28/2010 2:52 PM, Peter Firmstone wrote: > Well, at the absolute minimum the variable should be volatile, so any > changes are visible among all threads. > > Since increment is the only mutating method, this must be synchronized. > > This is a cheap form of multi read, single write threading, although one > must be careful, as this only works on primitives and immutable object > references, since only the reference itself is being updated. > > If it was a reference to a mutable object (or long), then all methods > would need to be synchronized. > > Cheers, > > Peter. > > Patricia Shanahan wrote: >> I've found something I think is a problem in >> com.sun.jini.test.impl.outrigger.matching.StressTest, but it does not >> seem to be the problem, or at least not the only problem, causing the >> test hang I'm investigating. It's difficult to test, so I'd like a >> review of my reasoning. This is a question for those who are familiar >> with the Java memory model. >> >> Incidentally, if we went to 1.5 as the oldest supported release, this >> could be replaced by an AtomicInteger. >> >> In the following inner class, I think the methods reset and getCount >> should be synchronized. As the comments note, the operations they >> perform are atomic. My concern is the lack of a happens-before >> relationship between those two methods and the increment method. As >> far as I can tell, there is nothing forcing the change in the counter >> due to an increment to become visible to a getCount call in another >> thread. >> >> private class Counter { >> >> /** >> * Internal counter variable. >> */ >> private int _count = 0; >> >> /** >> * Constructor. Declared to enforce protection level. >> */ >> Counter() { >> >> // Do nothing. >> } >> >> /** >> * Resets internal counter to zero. >> */ >> void reset() { >> >> // Integer assignment is atomic. >> _count = 0; >> } >> >> /** >> * Increments internal counter by one. >> */ >> synchronized void increment() { >> ++_count; >> } >> >> /** >> * Returns current value of this Counter object. >> */ >> int getCount() { >> >> // Returning an integer is atomic. >> return _count; >> } >> } >> > >