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


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

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

From Tom Anderson <twic@urchin.earth.li>
Newsgroups comp.lang.java.programmer
Subject Re: Question whether a problem with race conditions exists in this case
Date 2011-12-15 14:58 +0000
Organization Stack Usenet News Service
Message-ID <alpine.DEB.2.00.1112151445530.21168@urchin.earth.li> (permalink)
References <8bbbbee3-adcc-4f28-aff5-2e230b047401@u6g2000vbg.googlegroups.com> <slrnjehol1.fvg.avl@gamma.logic.tuwien.ac.at> <8946607.360.1323888252717.JavaMail.geo-discussion-forums@prfb7> <slrnjei4fi.fvg.avl@gamma.logic.tuwien.ac.at>

Show all headers | View raw


On Wed, 14 Dec 2011, Andreas Leitgeb wrote:

> From examples posted here during the last few years, I've come to think, 
> that one just cannot really understand concurrency and memory-barriers. 
> If one thinks he does, then he either missed the hairier bits, or just 
> doesn't (perhaps rightly so) believe them.

Really?

I have come to think that to understand concurrency and the effects of 
memory barriers, one must think *strictly* and *only* in terms of 
happens-before relationships, as specified by the JLS. The whole of 
section 17.4 is relevant, with 17.4.5 being the heart of the matter:

http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4

The rules for working out happens-before relationships are simple. At 
least, the fundamental idea is simple; the details of exactly when a 
relationship arises are less simple, but if you know the few most 
important ones, you will be fine most of the time. *Every Java programmer 
should know these rules*.

To paraphrase the JLS slightly, they are:

1. If x and y are actions of the same thread and x comes before y in 
program order, then x happens before y

('actions' mostly means reads and writes on memory, and a few other, less 
important things; 'program order' means the order of actions within a 
single thread)

2. If x happens before y, and y happens before z, then x happens before z

3. The release of a lock happens before any subsequent acquisition of that 
lock

4. A write to a volatile variable happens before any subsequent read of 
that variable

There are further rules about interruption, thread birth and death, and 
object initialisation, but they are significantly less important. I still 
think everyone ought to know them, but i will understand if they don't (i 
don't know the ones about interruption and thread lifecycle off the top of 
my head!).

From these rules, you can work out if there is a happens-before 
relationship between any two actions. Doing so is not necessarily trivial, 
but it is no more complex than, say, solving a sudoku problem. It's a 
question of applying the rules, and trying to find a chain of 
happens-before relationships. If you can't find one, you conclude one 
doesn't exist.

You don't need to know any more about memory barriers than that. The 
effects of memory barriers are as required by these rules.

tom

-- 
And the future is certain, give us time to work it out

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