Return-Path: Delivered-To: apmail-commons-dev-archive@www.apache.org Received: (qmail 93249 invoked from network); 28 Jan 2011 23:00:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 28 Jan 2011 23:00:28 -0000 Received: (qmail 43448 invoked by uid 500); 28 Jan 2011 23:00:27 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 43300 invoked by uid 500); 28 Jan 2011 23:00:27 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 43292 invoked by uid 99); 28 Jan 2011 23:00:27 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 Jan 2011 23:00:27 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of stephendwilliams@gmail.com designates 74.125.82.41 as permitted sender) Received: from [74.125.82.41] (HELO mail-ww0-f41.google.com) (74.125.82.41) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 Jan 2011 23:00:23 +0000 Received: by wwi18 with SMTP id 18so1553096wwi.0 for ; Fri, 28 Jan 2011 15:00:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type; bh=WGiEPLPjSGlrsTXF0mMosxWNHVXZlgz7ntK1llWF6RQ=; b=k6A9AfCIhWuNpfVciE/B44X4q5QQ4hB34RU45FxXi9IX1strXsomCL76+oQ6wgeIC3 GVr0IprTObH8qz8QprYLnJ63/ZbQaJs8Mkc0f/OnxTxrqwRKDhtOX8UCQMUlC3g1a50g OizwArrKN1Tc/oUodFzdot/r5X6F4hfkWboMQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type; b=lETUfZk0bkfIUTZhu0u6u7IVQdSpdoaz5fUxgP/qgF9uvWNEC6/hyoVMP8o54LkwzD ppOeTJ74pzDtTycoeLCnzKL8Ltm6vhI3dpp4o0Re1ju/lET7yvxs5xPO2aQoDY+vtBVN pBdhHY2xo8+yq0r7s7XK7PFOb3F2s51nbzbnc= MIME-Version: 1.0 Received: by 10.216.0.140 with SMTP id 12mr3762198web.29.1296255601529; Fri, 28 Jan 2011 15:00:01 -0800 (PST) Sender: stephendwilliams@gmail.com Received: by 10.216.60.68 with HTTP; Fri, 28 Jan 2011 15:00:01 -0800 (PST) In-Reply-To: References: <59E47574-D831-4376-A1A3-FB833350E628@gmail.com> Date: Fri, 28 Jan 2011 15:00:01 -0800 X-Google-Sender-Auth: j-zvYYx4QOy0Fp3utCaAik4a7_g Message-ID: Subject: Re: Pointers From: Stephen Williams To: Commons Developers List , sdw@lig.net Content-Type: multipart/alternative; boundary=001485f6d758aad248049af0061f --001485f6d758aad248049af0061f Content-Type: text/plain; charset=UTF-8 True, you shouldn't synchronize on any object that A) you want to change and B) can only be changed by being replaced by a newly constructed object. If Integer, for instance, had a setter method, then it could still have been used in this way. However, it seems to be immutable. I generally just create something like the following to synchronize: Object lockArg; A method I use for returning more than one value would be useful here also: Integer[] arg = new Integer[](1); arg[0] = new Integer(5); // or with autoboxing: arg[0] = 5; void funkyChanger(Integer[] arg) { synchronized (arg) { ... arg[0]++; ... } } Is there a problem with this as a general idiom? You could use naming to signal the nature as indirect vs. logically an array. And, a future version of Java could use syntactic sugar for 'ref' to do this by autoboxing the 1 deep array also. About returning tuples: Returning more than one value from a method has two reasonable solutions that seem minimal for their use cases: returning a tuple as a single value or making one or more calls to a callback method. The simplest case is when multiple values need to be returned. While callbacks have their place when you need to trigger more operations incrementally, they are cumbersome as you need to define a class and provide an implementation, although the latter can be an anonymous implementation. Many have complained about the lack in Java of pair<>, however with the object reference system, it is easy to do this with arrays. If the types are all the same, like String[], this is completely typesafe. Otherwise, you can use Object[]. You can also define a return type class to fill, however that is cumbersome. In addition to Object[], a TreeMap can be a good solution, especially when more than a few parameters are needed and they might change over time. Stephen On Fri, Jan 28, 2011 at 2:21 PM, James Ring wrote: > Hey, > > On Fri, Jan 28, 2011 at 2:01 PM, Stephen Williams > wrote: > > All objects are passed as references in Java. > > All fundamental scalar types have Object wrapped versions. > > An argument that is meant to be modified just needs to be an object > > reference. > > > > So, you can simply go from: > > void funkyReader(int arg) { arg++; } > > to: > > void funkyChanger(Integer arg) { arg++; } > > > > If it needs to be threadsafe, simply synchronize on the object everywhere > it > > is used. > > It's a bad idea to synchronize on the wrapper objects of primitive > types: http://www.theothertomelliott.com/node/40 > > This includes Integers. > > Regards, > James > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org > > -- -- Stephen D. Williams sdw@lig.net scienteer@gmail.com LinkedIn: http://sdw.st/in V:650-450-UNIX (8649) V:866.SDW.UNIX V:703.371.9362 F:703.995.0407 AIM:sdw Skype:StephenDWilliams Resume: http://sdw.st/gres Personal: sdw.st facebook.com/sdwlig twitter.com/scienteer --001485f6d758aad248049af0061f--