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)

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: reading the JLS (17.4.5)
Date 2011-12-20 19:08 -0800
Organization http://groups.google.com
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>

Show all headers | 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