Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
61 views
in Technique[技术] by (71.8m points)

java - How to deliver object to a long-running SwingWorker from the EDT?

How can the EDT communicate to an executing SwingWorker? There a lot of ways for the SwingWorker to communicate information back to the EDT - like publish/process and property changes but no defined way (that I have seen) to communicate in the other direction. Seems like good old Java concurrent inter-thread communication would be the way to go via wait() and notify(). This doesn't work. I'll explain later. I actually got it to work but it uses an ugly polling mechanism. I feel like there should be a better way. Here is the process that I am trying to accomplish:

  1. From the main Swing UI (EDT) a user starts a SwingWorker long-running task (the engine).
  2. At some point the engine needs information from the EDT so it communicates this back to the EDT. this could be done through publish/process update of a visible UI component. Importantly, this step DOES NOT block the EDT because other things are also going on.
  3. The engines blocks waiting for an answer.
  4. At some point the user notices the visual indication and provides the required information via some UI (EDT) functionality - like pressing a Swing button.
  5. The EDT updates an object on the engine. Then "wakes up" the engine.
  6. The engine references the updated object and continues to process.

The problem I have with wait()/notify() is that in step 3 any invocation of wait() in doInBackground() causes the done() method to be immediately fired and the SwingWorker to be terminated. I was able to get the above process to work by using an ugly sleep() loop in doInBackground():

for (;;)
{
    Thread.sleep(10);

    if (fromEDT != null)
    {
        // Process the update from the EDT
        System.out.println("From EDT: " + fromEDT);
        fromEDT = null;
        break;
    }
}

What this really is that in step 5 the engine wakes itself up and checks for updates from the EDT.

Is this the best way to do this? I kind of doubt it.

question from:https://stackoverflow.com/questions/66053132/how-to-deliver-object-to-a-long-running-swingworker-from-the-edt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following is an mre demonstrating a SwingWorker paused and waiting for user's input:

import java.awt.*;
import java.util.List;
import javax.swing.*;

public class SwingWorkerWaitDemo {

    public static void creategui(){

        JFrame f = new JFrame("SwingWorker wait Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.add(new MainPanel());
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        creategui();
    }
}

class MainPanel extends JPanel {

    private static final String BLANK = "               ";

    private MyWorker swingWorker;
    private final JLabel output, msg;
    private final JButton start, stop, respond;

    MainPanel() {
        setLayout(new BorderLayout(2, 2));
        start = new JButton("Start");
        start.addActionListener(e->start());
        stop = new JButton("Stop");
        stop.setEnabled(false);
        stop.addActionListener(e->stop());
        JPanel ssPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
        ssPane.add(start); ssPane.add(stop);
        add(ssPane, BorderLayout.PAGE_START);

        output = new JLabel(BLANK);
        JPanel outputPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
        outputPane.add(output);
        add(outputPane, BorderLayout.CENTER);

        msg = new JLabel(BLANK);
        respond = new JButton("Respond");
        respond.addActionListener(e->respond());
        respond.setEnabled(false);
        JPanel responsePane = new JPanel();
        responsePane.add(msg); responsePane.add(respond);
        add(responsePane, BorderLayout.PAGE_END);
    }

    @Override
    public Dimension getPreferredSize(){
        return new Dimension(400, 200);
    }

    private void start() {
        start.setEnabled(false);
        stop.setEnabled(true);
        swingWorker = new MyWorker();
        swingWorker.execute();
    }

    private void stop() {
        stop.setEnabled(false);
        swingWorker.setStop(true);
    }

    private void message(String s){
        msg.setText(s);
    }

    private void clearMessage(){
        msg.setText(BLANK);
    }

    private void askForUserResponse(){
        respond.setEnabled(true);
        message("Please respond " );
    }

    private void respond(){
        clearMessage();
        respond.setEnabled(false);
        swingWorker.setPause(false);
    }

    class MyWorker extends SwingWorker<Integer, Integer> {

        private boolean stop = false;
        private volatile boolean pause = false;

        @Override
        protected Integer doInBackground() throws Exception {

            int counter = 0;

            while(! stop){
                publish(counter++);
                if(counter%10 == 0) {
                    pause = true;
                    askForUserResponse();
                    while(pause){   /*wait*/ }
                }
                Thread.sleep(500);
            }
            return counter;
        }

        @Override
        protected void process(List<Integer> chunks) {
            for (int i : chunks) {
                output.setText(String.valueOf(i));
            }
        }

        @Override
        protected void done() {
            message("All done");
        }

        void setStop(boolean stop) {
            this.stop = stop;
        }

        void setPause(boolean pause) {
            this.pause = pause;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...