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


Groups > de.comp.lang.java > #13001

Re: GUI-Update über Swing-EDT

From Patrick Roemer <sangamon@netcologne.de>
Newsgroups de.comp.lang.java
Subject Re: GUI-Update über Swing-EDT
Date 2016-07-16 16:42 +0200
Organization news.netcologne.de
Message-ID <nmdh3u$91j$1@newsreader4.netcologne.de> (permalink)
References <duo028Fb4qaU1@mid.individual.net> <nm8j8b$r9c$1@newsreader4.netcologne.de> <durqo5F8hcgU1@mid.individual.net> <nmbhss$ub4$1@newsreader4.netcologne.de> <duuqu9F1a5U1@mid.individual.net>

Show all headers | View raw


Responding to Christian H. Kuhn:
> Genau. getName() ist in meiner Anwendung unkritisch, weil Namen von
> GUI-Komponenten nur im Konstruktor gesetzt und nie mehr verändert
> werden, da entstehen keine Konflikte, solange nicht lesend auf ein noch
> nicht fertig konstruiertes Objekt zugegriffen wird. Ich hoffe, dass die
> VM sowas verhindert und ein Objekt erst da ist, wenn der Konstruktor
> fertig ausgeführt ist?

Wenn es keine happens-before-Beziehung gibt: Nein. Beispiel:

"The most obvious reason is that the writes which initialize instance
and the write to the instance field can be reordered by the compiler or
the cache, which would have the effect of returning what appears to be a
partially constructed Something. The result would be that we read an
uninitialized object."
https://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#dcl

> 	StringBuilder nameBuilder = new StringBuilder();
>         try {
>             SwingUtilities.invokeAndWait(new Runnable() {
> 
>                 @Override
>                 public void run() {
>                     _parent.getName(); // das geht
>                     name = _parent.getName(); // das geht nicht
> 		    test.setObject(_parent.getName()); // das geht
> 		    staticName = _parent.getName(); // das geht
> 		    nameBuilder.append(_parent.getName()); // das geht
>                 }
>             });

Die letzten drei gehen alle nicht, weil wieder nicht garantiert ist,
dass der main-Thread die Resultate der im EDT ausgeführten Aktion je zu
sehen bekommt. (Schlimmstenfalls bekommt er sie nur teilweise zu sehen.)

public static <T> T fromEDT(Supplier<T> block)
    throws InterruptedException {
  final AtomicReference<T> ret = new AtomicReference<>();
  try {
    SwingUtilities.invokeAndWait(() -> ret.set(block.get()));
  }
  catch (InvocationTargetException exc) {
    Throwable cause = exc.getCause();
    if(cause instanceof Error) {
      throw (Error)cause;
    }
    if(cause instanceof RuntimeException) {
      throw (RuntimeException)cause;
    }
    // shouldn't happen - no checked exceptions from Supplier/Runnable
    throw new RuntimeException(cause);
  }
  return ret.get();
}

String name = SwingHelper.fromEDT(() -> _parent.getName());

Viele Grüße,
Patrick

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


Thread

JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-14 01:09 +0200
  Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-14 18:26 +0200
  Re: JUnit Test von JButton: Action wird nicht erkannt Patrick Roemer <sangamon@netcologne.de> - 2016-07-14 19:47 +0200
    Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-15 00:08 +0200
      Re: JUnit Test von JButton: Action wird nicht erkannt Patrick Roemer <sangamon@netcologne.de> - 2016-07-15 22:04 +0200
        Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-15 22:53 +0200
        Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-15 23:09 +0200
          Re: JUnit Test von JButton: Action wird nicht erkannt Patrick Roemer <sangamon@netcologne.de> - 2016-07-15 23:44 +0200
            Countdown Timer Design (was: JUnit Test von JButton: Action wird nicht erkannt) "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-16 23:44 +0200
              Re: Countdown Timer Design "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-17 12:44 +0200
              Re: Countdown Timer Design "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-17 15:49 +0200
                Re: Countdown Timer Design "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-17 17:03 +0200
                Re: Countdown Timer Design "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-19 15:59 +0200
        Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-19 14:59 +0200
          Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-19 16:06 +0200
          Re: JUnit Test von JButton: Action wird nicht erkannt Patrick Roemer <sangamon@netcologne.de> - 2016-07-19 18:59 +0200
            Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-19 22:35 +0200
              Re: JUnit Test von JButton: Action wird nicht erkannt Patrick Roemer <sangamon@netcologne.de> - 2016-07-20 13:00 +0200
                Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-23 20:36 +0200
                Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-23 23:15 +0200
          Re: JUnit Test von JButton: Action wird nicht erkannt Wanja Gayk <brixomatic@yahoo.com> - 2016-07-19 23:02 +0200
            Re: JUnit Test von JButton: Action wird nicht erkannt "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-20 12:33 +0200
    GUI-Update über Swing-EDT (was: JUnit Test von JButton: Action wird nicht erkannt) "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-15 12:03 +0200
      Re: GUI-Update über Swing-EDT Patrick Roemer <sangamon@netcologne.de> - 2016-07-15 22:43 +0200
        Re: GUI-Update über Swing-EDT "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-15 23:18 +0200
        Re: GUI-Update über Swing-EDT "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-16 15:24 +0200
          Re: GUI-Update über Swing-EDT Patrick Roemer <sangamon@netcologne.de> - 2016-07-16 16:42 +0200
            Re: GUI-Update über Swing-EDT "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-16 23:05 +0200
              Re: GUI-Update über Swing-EDT "Christian H. Kuhn" <qno-news@qno.de> - 2016-07-17 16:02 +0200

csiph-web