Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: static synchronized method Date: Wed, 27 Jul 2011 06:37:17 -0700 Organization: A noiseless patient Spider Lines: 19 Message-ID: References: <0e62fc71-0ae6-408c-9e93-c43b3b7c56f0@28g2000pry.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 27 Jul 2011 13:37:20 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="17514"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WZvUPrJ5KiKpGpTw6Nqtw4E/zMdFHLpc=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <0e62fc71-0ae6-408c-9e93-c43b3b7c56f0@28g2000pry.googlegroups.com> Cancel-Lock: sha1:/Nzgeo3js9FvGpAj/MCwBOQjWVk= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6612 On 7/27/2011 6:31 AM, Ross wrote: > If I have a method which is both static and synchronized, then can I > guarantee that only one thread will be allowed in at once, even if I > have multiple instances of the object in memory, one per thread? Yes. The "synchronized" keyword on a static method synchronizes on the class object. Since all instances of that class share one class object, then there'll be only one thread, period, in that method at a time. There's a slight exception for class loaders which load load multiple instances of the same class object. Since all the static method does is synchronize on the class object, there may be be more than one thread, one per loaded class object, in those circumstances. There are some good reasons to have multiple class objects, but it's not really normal and would usually be considered an error in the class loader, just so you know.