0

Im going through concurrency documentation and I can't quite understand what they mean with:

In an applet, the GUI-creation task must be launched from the init method using invokeAndWait; otherwise, init may return before the GUI is created, which may cause problems for a web browser launching an applet. In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses invokeLater or invokeAndWait.'

-What is the problem with the init being returned before GUI-creation? -Why is the GUI-creation usually the last thing a thread does?

Tasks on the event dispatch thread must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive.'

-How can you make it finish faster?

http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TumbleItemProject/src/components/TumbleItem.java

-Where is the EDT in the above example?

SwingWorker worker = new SwingWorker<ImageIcon[], Void>() {
@Override
public ImageIcon[] doInBackground() {
    final ImageIcon[] innerImgs = new ImageIcon[nimgs];
    for (int i = 0; i < nimgs; i++) {
        innerImgs[i] = loadImage(i+1);
    }
    return innerImgs;
}

@Override
public void done() {
    //Remove the "Loading images" label.
    animator.removeAll();
    loopslot = -1;
    try {
        imgs = get();
    } catch (InterruptedException ignore) {}
    catch (java.util.concurrent.ExecutionException e) {
        String why = null;
        Throwable cause = e.getCause();
        if (cause != null) {
            why = cause.getMessage();
        } else {
            why = e.getMessage();
        }
        System.err.println("Error retrieving file: " + why);
    }
}

};

-Why is initialisation of 'worker' here followed by the overwriting of several methods instead of just ';'? I have never seen this kind of notation before... -Are all methods that aren't the 'doInBackGround()' method ,executed under the event dispatch thread?

'All concrete subclasses of SwingWorker implement doInBackground; implementation of done is optional.'

-In the code example ,I don't see a subclass for SwingWorker , unless new SwingWorker <>() , counts as a subclass?

'Be careful when invoking either overload of get from the event dispatch thread; until get returns, no GUI events are being processed, and the GUI is "frozen". Don't invoke get without arguments unless you are confident that the background task is complete or close to completion.'

-How would you use get() in a non-EDT way ?

My apologies if some questions are obvious and thank you for your time!

BURNS
  • 711
  • 1
  • 9
  • 20
  • *"In the code example ,I don't see a subclass for SwingWorker , unless new SwingWorker <>() , counts as a subclass?"* http://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java – m0skit0 Feb 23 '14 at 23:42
  • Regarding #2; it's not about making the threads run faster, it's rather that if you have a long-going task, do it in a background thread instead of the event (GUI) thread as to not make it freeze. – Patrick Feb 23 '14 at 23:42
  • Regarding #1: It says it right there in the text. "It might cause problems for browsers". It *may* be because the caller of init requires the method to create a GUI and schedule it for gui messages, and if none is created the caller fails. – Patrick Feb 23 '14 at 23:46

1 Answers1

1

What is the problem with the init being returned before GUI-creation? -Why is the GUI-creation usually the last thing a thread does?

It says it right there in the text. "It might cause problems for browsers". It may be because the caller of init requires the method to create a GUI and schedule it for gui messages, and if none is created the caller fails.

How can you make it finish faster?

It's not about making the threads run faster, it's rather that if you have a long-going task, do it in a background thread instead of the event (GUI) thread as to not make it freeze.

Where is the EDT in the above example?

It's nowhere.. The applet has a EDT which is used when you for instance click a button or interact with the applet in other ways. I don't really understand your question here.

Why is initialisation of 'worker' here followed by the overwriting of several methods instead of just ';'? I have never seen this kind of notation before... -Are all methods that aren't the 'doInBackGround()' method ,executed under the event dispatch thread?

It's an anonymous class declaration of the SwitchWorker class. No, the doInBackground does not execute on the EDT, it's performed in the background. done is however scheduled on the EDT. See http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html for reference.

In the code example ,I don't see a subclass for SwingWorker , unless new SwingWorker <>() , counts as a subclass?

It does.

How would you use get() in a non-EDT way ?

As the documentation specifies:

get()

Waits if necessary for the computation to complete, and then retrieves its result.

So you should not call it until the background method completes, as to avoid freezing of the GUI.

Patrick
  • 17,669
  • 6
  • 70
  • 85