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


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

Re: reading the JLS (17.4.5)

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: reading the JLS (17.4.5)
Date Tue, 20 Dec 2011 19:08:01 -0800 (PST)
Organization http://groups.google.com
Lines 37
Message-ID <17674291.80.1324436881159.JavaMail.geo-discussion-forums@prez5> (permalink)
References <slrnjerm1c.fvg.avl@gamma.logic.tuwien.ac.at> <wOWdnRqBm-mNvHPTnZ2dnUVZ_omdnZ2d@earthlink.com> <slrnjf1lan.fvg.avl@gamma.logic.tuwien.ac.at>
Reply-To comp.lang.java.programmer@googlegroups.com
NNTP-Posting-Host 2620:0:1000:fd2b:224:d7ff:fe69:5838
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1324437327 2234 127.0.0.1 (21 Dec 2011 03:15:27 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Wed, 21 Dec 2011 03:15:27 +0000 (UTC)
In-Reply-To <slrnjf1lan.fvg.avl@gamma.logic.tuwien.ac.at>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:fd2b:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10924

Show key headers only | View raw


Andreas Leitgeb wrote:
> Given some (simplified) sample code:
>   class Test {
>      /*non-vol*/ int n1;
>      volatile    int v1;
> 
>      void t1() {
>        n1 = 1;
>        v1 = n1; // v1 uses n1
>      }
> 
>      void t2() {
>        int r1=v1, r2=r1*n1; // r2 uses r1
>        assert ! (r1 == 1 && r2 != 1) : "huh?";
>      }
>   }
> Two threads T1 and T2 may at some point run t1() and t2()
> respectively, then I should expect, per transitivity of

You have to establish a /happens-before/ between the invocations of t1() an t2().

If two threads begin to execute the methods, you cannot guarantee that t1() will /happen-before/ t2(), so the read of 'v1' in the latter could result in the default value.

-- 
Lew

> "happens-before" that if r1 == 1, then r2 would *have to*
> == 1, too.  Is there a happens-before relation between
> between setting n1 and reading n1 via write&read on the
> volatile v1 and each intra-thread ordering?
> 
> There is a slightly similar example later in the JLS about
> a final and a non-final set in a constructor, but that is
> different, in that the non-final is set after the final.
> I don't know for sure, if the ordering of the assignments
> was relevant in that example.

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


Thread

reading the JLS (17.4.5) Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-18 12:10 +0000
  Re: reading the JLS (17.4.5) markspace <-@.> - 2011-12-18 06:57 -0800
    Re: reading the JLS (17.4.5) Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-20 17:54 +0000
      Re: reading the JLS (17.4.5) markspace <-@.> - 2011-12-20 10:50 -0800
        Re: reading the JLS (17.4.5) Patricia Shanahan <pats@acm.org> - 2011-12-20 12:12 -0800
          Re: reading the JLS (17.4.5) Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-21 08:54 +0000
            Re: reading the JLS (17.4.5) Patricia Shanahan <pats@acm.org> - 2011-12-21 10:56 -0800
              Re: reading the JLS (17.4.5) markspace <-@.> - 2011-12-21 12:02 -0800
  Re: reading the JLS (17.4.5) Patricia Shanahan <pats@acm.org> - 2011-12-18 09:21 -0800
    Re: reading the JLS (17.4.5) Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-20 18:35 +0000
      Re: reading the JLS (17.4.5) Lew <lewbloch@gmail.com> - 2011-12-20 19:08 -0800
        Re: reading the JLS (17.4.5) Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-12-21 08:37 +0000
          Re: reading the JLS (17.4.5) Patricia Shanahan <pats@acm.org> - 2011-12-21 10:46 -0800
          Re: reading the JLS (17.4.5) markspace <-@.> - 2011-12-21 12:09 -0800

csiph-web