Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Pause and Resume SwingWorker.doInBackground()

I have a basic Swing UI with a single button marked "Play." When the button is pressed the label changes to "Pause". When the button is pressed now it changes to say "Resume."

On "Play" I am instantiating and executing a SwingWorker. What I would like is to be able to pause this thread (NOT cancel it) and resume it according to the button presses described above. However, I'd prefer not to resort to Thread.sleep() in doInBackground(). That seems a bit hackish. Is there any way for the thread running doInBackground to block?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Pause and Resume SwingWorker.doInBackground()

First of all you have to be sure the background task being performed can be paused, otherwise the question doesn't make sense. So let's say the task can be paused, then you might extend SwingWorker class and make your own pausable worker using a simple flag variable to control the background thread status: paused or not paused.

public abstract class PausableSwingWorker<K, V> extends SwingWorker<K, V> {

    private volatile boolean isPaused;

    public final void pause() {
        if (!isPaused() && !isDone()) {
            isPaused = true;
            firePropertyChange("paused", false, true);
        }
    }

    public final void resume() {
        if (isPaused() && !isDone()) {
            isPaused = false;
            firePropertyChange("paused", true, false);
        }
    }

    public final boolean isPaused() {
        return isPaused;
    }
}

Subclasses might check isPaused() status in order to efectively proceed with the task or not. For example:

PausableSwingWorker<Void, String> worker = new PausableSwingWorker<Void, String>() {
    @Override
    protected Void doInBackground() throws Exception {
        while (!isCancelled()) {
            if (!isPaused()) {
                // proceed with background task
            } else {
                Thread.sleep(200); // Optional sleep to avoid check status continuously
            }
        }
        return null;
    }
};

You can also add a PropertyChangeListener to the worker and listen for paused property changes:

worker.addPropertyChangeListener(new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("paused".equals(evt.getPropertyName())) {
           System.out.println("Old status: " + evt.getOldValue());
           System.out.println("New status: " + evt.getNewValue());
        }
    }
});

Example (updated to make use of PropertyChangeListener)

Here is a complete example to play with. Please note that if worker is stopped then it cannot be paused nor resumed anymore.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class Demo {

    private void createAndShowGUI() {

        final JTextArea textArea = new JTextArea(20, 50);

        final PausableSwingWorker<Void, String> worker = new PausableSwingWorker<Void, String>() {
            @Override
            protected Void doInBackground() throws Exception {
                while (!isCancelled()) {
                    if (!isPaused()) {
                        publish("Writing...");
                    } else {
                        Thread.sleep(200);
                    }
                }
                return null;
            }

            @Override
            protected void process(List<String> chunks) {
                String text = String.format("%s%n", chunks.get(chunks.size() - 1));
                textArea.append(text);
            }
        };

        worker.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("paused".equals(evt.getPropertyName())) {
                    String text = (Boolean)evt.getNewValue() ? "Paused..." : "Resumed...";
                    textArea.append(String.format("%s%n", text));
                }
            }
        });

        Action pause = new AbstractAction("Pause") {
            @Override
            public void actionPerformed(ActionEvent e) {
                worker.pause();
            }
        };

        Action resume = new AbstractAction("Resume") {
            @Override
            public void actionPerformed(ActionEvent e) {
                worker.resume();
            }
        };

        Action stop = new AbstractAction("Stop") {
            @Override
            public void actionPerformed(ActionEvent e) {
                worker.cancel(true);
            }
        };

        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonsPanel.add(new JButton(pause));
        buttonsPanel.add(new JButton(resume));
        buttonsPanel.add(new JButton(stop));

        JPanel content = new JPanel(new BorderLayout(8, 8));
        content.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        content.add(new JScrollPane(textArea), BorderLayout.CENTER);
        content.add(buttonsPanel, BorderLayout.SOUTH);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (!worker.isDone()) {
                    worker.cancel(true);
                }
                e.getWindow().dispose();
            }
        });

        frame.add(content);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        worker.execute();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGUI();
            }
        });
    }

    abstract class PausableSwingWorker<K, V> extends SwingWorker<K, V> {

        private volatile boolean isPaused;

        public final void pause() {
            if (!isPaused() && !isDone()) {
                isPaused = true;
                firePropertyChange("paused", false, true);
            }
        }

        public final void resume() {
            if (isPaused() && !isDone()) {
                isPaused = false;
                firePropertyChange("paused", true, false);
            }
        }

        public final boolean isPaused() {
            return isPaused;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...