Re: Are there real use cases with the Java access modes?

Valentin Kovalenko via Concurrency-interest <[email protected]> Mon, 19 Jul 2021 10:01:03 -0600
Newsgroups gmane.comp.java.jsr.166-concurrency
Message-ID <CAO-wXwL7Cz2oeCO1mO1QTg7L-AcB91mshhegQ4+j-s5McMiT2A@mail.gmail.com>
Hi Gregg,

> There is no explicit indication of Thread usage

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.

Regards,
Valentin www.kovalenko.link


>
> Message: 4
> Date: Mon, 19 Jul 2021 09:17:05 -0500
> From: Gregg Wonderly <[email protected]>
> To: Alex Otenko <[email protected]>
> Cc: concurrency-interest <[email protected]>
> Subject: Re: [concurrency-interest] Are there real use cases with the
>         Java access modes?
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="utf-8"
>
> Here’s what I’ve repeatedly shown in these conversations in the past.
> There is no explicit indication of Thread usage.  This is just a plain and
> simple swing application that would be the kind of framework that the first
> Swing application would be written using.  But even some of the things I am
> doing here are a bit more than what I would expect to see in someones first
> swing app.
>
> Gregg Wonderly
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.event.*;
> import java.util.logging.*;
>
> public class Example2 extends JFrame {
>
>         JTextField tm;
>         JButton btn;
>         boolean done;
>         public static void main( String args[] ) {
>                 new Example2(args).open();
>                 System.exit(0);
>         }
>
>         public Example2(String args[] ) {
>                 JPanel p = new JPanel();
>                 p.setLayout( new FlowLayout() );
>                 p.add( btn = new JButton("Press to Exit") );
>                 p.add( tm = new JTextField(20) );
>                 this.add(p);
>                 btn.addActionListener( new ActionListener() {
>                         public void actionPerformed( ActionEvent ev ) {
>
> Logger.getLogger(Example2.class.getName()).info("done...");
>                                 done = true;
>                         }
>                 });
>                 done = false;
>         }
>
>         public void open() {
>                 pack();
>                 setLocationRelativeTo(null);
>                 setVisible(true);
>
>                 // Does your JVM convert this statement to if( !done ) {
> while( true ){ ...  } }?
>                 while( !done ) {
>                         tm.setText(""+new java.util.Date() );
>                         try {
>                                 Thread.sleep(500);
>                         } catch( Exception ex ) {
>                                 Logger.getLogger(Example2.class.getName())
>                                         .log( Level.SEVERE,
> ex.getMessage(), ex );
>                         }
>                 }
>         }
> }
>
>

_______________________________________________
Concurrency-interest mailing list
[email protected]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest