X-Received: by 10.224.52.6 with SMTP id f6mr9879603qag.2.1377099376193; Wed, 21 Aug 2013 08:36:16 -0700 (PDT) X-Received: by 10.50.78.1 with SMTP id x1mr137504igw.2.1377099376157; Wed, 21 Aug 2013 08:36:16 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!fx3no3898786qab.0!news-out.google.com!he10ni2777qab.0!nntp.google.com!fx3no3898780qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Wed, 21 Aug 2013 08:36:15 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T NNTP-Posting-Host: 173.164.137.214 References: <52140491$0$299$14726298@news.sunsite.dk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Autoboxing and Performance ? From: Lew Injection-Date: Wed, 21 Aug 2013 15:36:16 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com comp.lang.java.programmer:25092 Joerg Meier wrote: > Eric Sosman wrote: >> Nonetheless, I'm still of the opinion that autoboxing is Satan's >> work. Another thread, somewhat further back, wondered why this >> apparently thread-safe code wasn't thread-safe at all: > >> class Whatever { >> private static Integer counter; >> void someMethod() { >> synchronized(counter) { >> ++counter; >> ... >> } >> } >> ... >> } > > I still don't see how that wouldn't be thread-safe, could you elaborate ? A monitor has to be the same object between threads. Holding a "lock" on two different objects actually doesn't lock anything. > Or are we to assume other code touches counter without being in a If 'counter' changes to point to a different object, as in the sample code here, then it no longer points to the object holding the monitor, which makes it useless as a lock. > synchronized(counter) block itself in this example ? The finger is not the moon. That means a pointer is not the same thing as the object to which it points. Locks are taken on objects, not pointers. -- Lew