Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: BGB Newsgroups: comp.lang.java.programmer Subject: Re: Why "lock" functionality is introduced for all the objects? Date: Tue, 28 Jun 2011 13:43:54 -0700 Organization: albasani.net Lines: 104 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net zSUa6mf8RKLA3k2fzAc7zJZuEq0BHfOu3qv6OwgQnSSzVYJxqMZ+2PBgq5vy/ywnA+Rj5/I3OqnOfhiZdf8mSCHuTsHS1J2f9MEEqIMHG1NxPHRHnrscQz2e9dA8kB5q NNTP-Posting-Date: Tue, 28 Jun 2011 20:47:47 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="123p8mUTHDGiCYld5M/auvt1/KJKOcm0RV98I7XkrodNGYfzmEniauKoXEpEeHcVGQJMtCNALqE9Wp95Ep62NLyK9jXV3YHP+URNTH538WL0gZLfbCxExpSGKpF8FR1t"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 In-Reply-To: Cancel-Lock: sha1:vkiXm8WUqhB8H12sucs2yEaC0no= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5753 On 6/28/2011 9:41 AM, Michal Kleczek wrote: > Lew wrote: > >> Alex J wrote: >>> I'm curious why Java designers once decided to allow every object to >>> be lockable (i.e. [sic] allow using lock on those). >> >> Because that makes it possible to do concurrent programming intrinsically. >> > > Could you elaborate more on that? > Do you mean there is no other way to do it? > > I find this question quite intriguing as well since it looks quite useless > for example to be able to lock on java.lang.Integer instance (and it is > strange for me that java.lang.Integer instance occupies much more memory as > int). Surely a compromise must have been done taking into account various > language features ("synchronized" keyword, lack of multiple inheritance, > lack of closures) - but I am not that knowlegeable enough to explain this > compromise in detail. > yeah... they made every object lockable but not every object cloneable, where one would think cloning would be generally a higher priority. I guess the alternative would require, say: class Foo implements Lockable { ... } or at least: synchronized class Foo { } although this could be confusing/misleading. >>> I know, that out of such a design decision every Java object contain >>> lock index, i.e. new Object() results in allocation of at least 8 >>> bytes where 4 bytes is object index and 4 bytes is lock index on 32- >>> bit JVM. >>> I think that it just inefficient waste of space, because not all the >>> objects requires to be lockable/waitable. >> >> Well, that's your opinion. >> > > It is not only his opinion - the size of object header is important > especially on memory constrained devices. But not only - there is a reason > why UseCompressedOops flag was introduced in 64 bit HotSpot JVM. > there are also other possible implementation strategy (used in my own VM) where as opposed to a fixed per-object overhead, lock/wait/... are implemented internally via a magic table: locked objects may be added to a table, and waiting threads may be suspended and added to a second table linked to the object in the first; when a status change occurs, then the waiting threads are woken up, at which point they may do whatever (perform the operation, try to lock the object again, ...), and if they try to lock the object again and can't get a lock right away, then they go back to sleeping. this strategy trades off some performance for not having to store per-object lock state (especially given this is a relatively rare operation IME). >>> The better decision, IMHO, would be to introduce lock/wait mechanics >>> for only, say, the Lockable descendants. >> >> Oh, yeah, your opinion is humble. >> >>> The current approach seems to be very simple, but is the performance >>> penalty so small for not be taken into an account? >> >> Yes. Nonexistent, really. >> > > I wouldn't say so - see: > http://wikis.sun.com/display/HotSpotInternals/CompressedOops > Especially the sentence: > "Memory is pretty cheap, but these days bandwidth and cache is in short > supply" > yeah... another bigger problem though is when one has multiple memory-hungry apps compete for the available memory (say, one has several apps each eating 1GB-3GB of RAM, and one only has 4GB), and one is left "computing at the speed of swap" especially when one only has 5400RPM drives... in this case, smaller programs perform faster, as even when the rest of the system is bogged down with "the red HD light of doom", the app still keeps going ok mostly because its smaller footprint is like prone to getting swapped out, and because when it does get swapped, it can also pull things back in from disk much faster.