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 <s...@jdns.org> wrote: > Hey, > > On Fri, Jan 28, 2011 at 2:01 PM, Stephen Williams <scient...@gmail.com> > 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-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > > -- -- Stephen D. Williams s...@lig.net scient...@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