Re: Are there real use cases with the Java access modes?
Gregg Wonderly via Concurrency-interest <[email protected]> Thu, 22 Jul 2021 22:29:35 -0500
| Newsgroups | gmane.comp.java.jsr.166-concurrency |
|---|---|
| Message-ID | <[email protected]> |
--===============4202255206999176418== Content-Type: multipart/alternative; boundary="Apple-Mail=_51EEC27A-3B05-4660-9774-CE1F3F3A00D5" --Apple-Mail=_51EEC27A-3B05-4660-9774-CE1F3F3A00D5 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jul 19, 2021, at 11:01 AM, Valentin Kovalenko via = Concurrency-interest <[email protected]> wrote: > There is only one explanation I can think of with regard to how the = code in the listener added via `addActionListener` can be run: by a = `java.lang.Thread` different from the one running `main` and blocked in = `open` (this statement remains true even if one thinks about the project = Loom). If an engineer writing the code like that does not even question = himself about how can this application in principle be run in a single = thread (that's the assumption in the example), then eliminating some = optimizations that clearly do not violate JMM helps nothing. That same = engineer can then pass between threads not a single `boolean`, but a = more complex piece of data the same way with the same assumption (that = there is only one thread), and be surprised to the same extent to = observe not the piece of data that was shared. In fact, I think that the = more often incorrectly synchronized code fails, the more likely it is = that the author will discover and fix the bug, potentially improving his = understanding along the way. Those here, have this thought process already burned into our heads. It = seems likely to people who have experience in computer science training = and more formal exposure to concurrent software systems. The audience = I am trying to point out would have little chance to understand these = details. They understand logic, and can readily read documentation that = they can find. The =E2=80=9Chole=E2=80=9D I am trying to illustrate is = that the requirement for volatile, as the least amount of actual code to = make this example work, is not something they would readily know to = specify in a google search even. If you try yourself to search for = =E2=80=9Cloop doesn=E2=80=99t exit=E2=80=9D or =E2=80=9Cjava loop = doesn=E2=80=99t exit=E2=80=9D you will be hard pressed to find = discussion of volatile, based on what I=E2=80=99ve experienced trying to = see if there is something that I=E2=80=99ve just missed out on. = Instead, you have to know that the key is to include volatile. If you = don=E2=80=99t specify =E2=80=98java=E2=80=99 in your search, you=E2=80=99l= l get tons of results about C#, C++ and C ahead of Java, and the example = is about stopping a thread by monitoring a boolean, not ending a stuck = loop, but it does include volatile use which may be instructive = (https://www.java67.com/2015/07/how-to-stop-thread-in-java-example.html = <https://www.java67.com/2015/07/how-to-stop-thread-in-java-example.html>).= Is there anything else I haven=E2=80=99t beat to death about this? Gregg= --Apple-Mail=_51EEC27A-3B05-4660-9774-CE1F3F3A00D5 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; = charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; = -webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><br = class=3D""><div><br class=3D""><blockquote type=3D"cite" class=3D""><div = class=3D"">On Jul 19, 2021, at 11:01 AM, Valentin Kovalenko via = Concurrency-interest <<a = href=3D"mailto:[email protected]" = class=3D"">[email protected]</a>> wrote:</div><div = class=3D""><div dir=3D"ltr" class=3D""><div class=3D"">There is only one = explanation I can think of with regard to how the code in the listener = added via `addActionListener` can be run: by a `java.lang.Thread` = different from the one running `main` and blocked in `open` (this = statement remains true even if one thinks about the project Loom). If an = engineer writing the code like that does not even question himself about = how can this application in principle be run in a single thread (that's = the assumption in the example), then eliminating some optimizations that = clearly do not violate JMM helps nothing. That same engineer can then = pass between threads not a single `boolean`, but a more complex piece of = data the same way with the same assumption (that there is only one = thread), and be surprised to the same extent to observe not the piece of = data that was shared. In fact, I think that the more often incorrectly = synchronized code fails, the more likely it is that the author will = discover and fix the bug, potentially improving his understanding along = the way.</div></div></div></blockquote><br class=3D""></div><div>Those = here, have this thought process already burned into our heads. It = seems likely to people who have experience in computer science training = and more formal exposure to concurrent software systems. The = audience I am trying to point out would have little chance to understand = these details. They understand logic, and can readily read = documentation that they can find. The =E2=80=9Chole=E2=80=9D I am = trying to illustrate is that the requirement for volatile, as the least = amount of actual code to make this example work, is not something they = would readily know to specify in a google search even. If you try = yourself to search for =E2=80=9Cloop doesn=E2=80=99t exit=E2=80=9D or = =E2=80=9Cjava loop doesn=E2=80=99t exit=E2=80=9D you will be hard = pressed to find discussion of volatile, based on what I=E2=80=99ve = experienced trying to see if there is something that I=E2=80=99ve just = missed out on. Instead, you have to know that the key is to = include volatile. If you don=E2=80=99t specify =E2=80=98java=E2=80=99= in your search, you=E2=80=99ll get tons of results about C#, C++ and C = ahead of Java, and the example is about stopping a thread by monitoring = a boolean, not ending a stuck loop, but it does include volatile use = which may be instructive (<a = href=3D"https://www.java67.com/2015/07/how-to-stop-thread-in-java-example.= html" = class=3D"">https://www.java67.com/2015/07/how-to-stop-thread-in-java-examp= le.html</a>).</div><div><br class=3D""></div><div>Is there anything else = I haven=E2=80=99t beat to death about this?</div><div><br = class=3D""></div><div>Gregg</div></body></html>= --Apple-Mail=_51EEC27A-3B05-4660-9774-CE1F3F3A00D5-- --===============4202255206999176418== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KQ29uY3VycmVu Y3ktaW50ZXJlc3QgbWFpbGluZyBsaXN0CkNvbmN1cnJlbmN5LWludGVyZXN0QGNzLm9zd2Vnby5l ZHUKaHR0cDovL2NzLm9zd2Vnby5lZHUvbWFpbG1hbi9saXN0aW5mby9jb25jdXJyZW5jeS1pbnRl cmVzdAo= --===============4202255206999176418==--