Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10802

Re: Question whether a problem with race conditions exists in this case

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: Question whether a problem with race conditions exists in this case
References <8bbbbee3-adcc-4f28-aff5-2e230b047401@u6g2000vbg.googlegroups.com> <jcb49r$350$1@dont-email.me> <ebfc6764-d67a-43c5-9968-22ccad3ba1fa@x7g2000yqb.googlegroups.com> <alpine.DEB.2.00.1112151439460.21168@urchin.earth.li> <25969317.245.1324052869840.JavaMail.geo-discussion-forums@prfc16>
Message-ID <ZoLGq.18460$mJ.18419@newsfe10.iad> (permalink)
Date 2011-12-16 09:41 -0800

Show all headers | View raw


On 12/16/11 8:27 AM, Lew wrote:
> On Thursday, December 15, 2011 6:44:39 AM UTC-8, Tom Anderson wrote:
>> On Wed, 14 Dec 2011, Saxo wrote:
>>
>>> On Dec 14, 10:26 pm, Eric Sosman<esos...@ieee-dot-org.invalid>  wrote:
>>>
>>>>>     private Object lock = new Object();
>>>>
>>>>       What does `lock' buy you?  Why not just synchronize on the
>>>> Node itself?
>>>
>>> The purpose is only to indicate that some more fine-gtrained locking
>>> would be used for the real thing instead of doing a synchronized(this)
>>> { ... } thing.
>>
>> It's quite a common pattern. I'm always a bit dubious about using an
>> public object (FSVO 'public') as the victim of a synchronized block; how
>> do i know some random other bit of code in some other thread isn't going
>> to try to lock the object at some point, and cause trouble? You wouldn't
>> expose a field, would you? So why expose an object's lock? Essentially, i
>> see an object's lock as a feature, like a method or a field; it should
>> only be exposed to other classes after due consideration, and if it is,
>> its proper use should be documented.
>
> I control that by who sees the object, e.g., a 'Collections.synchronizedList()'.
>
> I see the point in what you're saying but I find it over-cautious sometimes..
> It depends on whether you want the object to control its own internal locking,
> which sometimes you do, or to be part of its client's thread control, as the numerous standard API classes with 'synchronized' methods do.
I seem to recall this practice of using an internal lock object being 
advocated in JCIP, but I'm not near my bookshelf at the moment so I 
can't be sure.

I happen to agree with Eric on this one though. If you expose any method 
as "synchronized" (whether it is public or not), then you have added an 
implicit contract to your object that you may not have intended.  That 
contract is "If *any* external process synchronizes on this object, it 
will be serialized with certain operations I perform."

More often than not, you will want to be explicit about behavior in 
concurrent situations, not implicit.
>
>> The locking analogue of a private field is an object like the above,
>> created for the sole purpose of supplying a lock that is provably only
>> accessible to code which can see the private details of the class.
>>
>> I have coined the name 'lockguffin' for these objects, and i encourage you
>> all to use it.
>
> When appropriate.
"When appropriate" should be the default modifier for all statements, 
because you shouldn't ever do something when its not appropriate :-)

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 09:07 -0800
  Re: Question whether a problem with race conditions exists in this case Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-14 17:53 +0000
    Re: Question whether a problem with race conditions exists in this case Lew <lewbloch@gmail.com> - 2011-12-14 10:44 -0800
      Re: Question whether a problem with race conditions exists in this case Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-14 10:54 -0800
      Re: Question whether a problem with race conditions exists in this case Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-14 21:15 +0000
        Re: Question whether a problem with race conditions exists in this case Tom Anderson <twic@urchin.earth.li> - 2011-12-15 14:58 +0000
          Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-15 08:40 -0800
          Re: Question whether a problem with race conditions exists in this case Gene Wirchenko <genew@ocis.net> - 2011-12-15 11:55 -0800
            Re: Question whether a problem with race conditions exists in this case Tom Anderson <twic@urchin.earth.li> - 2011-12-16 14:06 +0000
              Re: Question whether a problem with race conditions exists in this case Gene Wirchenko <genew@ocis.net> - 2011-12-16 10:09 -0800
    Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 11:47 -0800
  Re: Question whether a problem with race conditions exists in this case Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-14 10:53 -0800
    Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 11:54 -0800
      Re: Question whether a problem with race conditions exists in this case Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-15 16:38 -0800
        Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-15 23:01 -0800
          Re: Question whether a problem with race conditions exists in this case Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-16 09:34 -0800
            Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-17 11:55 -0800
              Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-19 05:33 -0800
  Re: Question whether a problem with race conditions exists in this case Lew <lewbloch@gmail.com> - 2011-12-14 11:04 -0800
    Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 12:32 -0800
      Re: Question whether a problem with race conditions exists in this case markspace <-@.> - 2011-12-14 14:13 -0800
        Re: Question whether a problem with race conditions exists in this case Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-14 17:44 -0500
          Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 14:50 -0800
            Re: Question whether a problem with race conditions exists in this case markspace <-@.> - 2011-12-14 15:26 -0800
      Re: Question whether a problem with race conditions exists in this case Lew <lewbloch@gmail.com> - 2011-12-15 01:34 -0800
    Re: Question whether a problem with race conditions exists in this case Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-14 21:38 +0000
  Re: Question whether a problem with race conditions exists in this case Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-14 16:26 -0500
    Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 13:57 -0800
      Re: Question whether a problem with race conditions exists in this case Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-14 18:05 -0500
        Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 23:25 -0800
          Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 23:28 -0800
      Re: Question whether a problem with race conditions exists in this case Tom Anderson <twic@urchin.earth.li> - 2011-12-15 14:44 +0000
        Re: Question whether a problem with race conditions exists in this case Lew <lewbloch@gmail.com> - 2011-12-16 08:27 -0800
          Re: Question whether a problem with race conditions exists in this case Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-16 09:41 -0800
            Re: Question whether a problem with race conditions exists in this case Tom Anderson <twic@urchin.earth.li> - 2011-12-16 18:51 +0000
          Re: Question whether a problem with race conditions exists in this case Tom Anderson <twic@urchin.earth.li> - 2011-12-16 18:50 +0000
    Re: Question whether a problem with race conditions exists in this case Saxo <saxo123@gmx.de> - 2011-12-14 14:13 -0800
      Re: Question whether a problem with race conditions exists in this case Lew <lewbloch@gmail.com> - 2011-12-14 16:55 -0800

csiph-web