Search code examples
javamultithreadingswingevent-dispatch-threadinvokelater

Cannot update Swing component under a heavy process


I am running a very heavy process under an anonymous SwingWorker thread. In the meantime, I'm reporting progress to the GUI using a progress bar. However, Swing threading is doing me in. It's simply not updating anything in time. I'm not sure how to do it, as I've tried updating the GUI from the SwingWorker thread, and outside, and both refuse to work.

How can I reliably update the Swing UI while a heavy worker thread is running?

Things I've tried

This does not work (with or without wrapping in the invokeLater command).

new LocalCompressor(compressor).execute();

while (!compressionDone) {
    SwingUtilities.invokeLater(new Runnable() {
    
        @Override
        public void run() {
            int percent = compressor.getPercentDone();
            progressBar.setValue(percent);
            statusLabel.setText(percent);
        }
    });
}

Additionally, attempting to update the UI from a concurrent measuring thread does not work:

class LocalCompressor extends SwingWorker<Void, Void> {

    // [...]
    
    public LocalCompressor(Compressor compressor) {
        this.compressor = compressor;
        
        // [...]
    }
    
    @Override
        protected Void doInBackground() {
        
            final Thread t1 = new Thread(new Runnable() {
            
                @Override 
                public void run(){
                    compressor.compress();
                }
            });
            
            final Thread t2 = new Thread(new Runnable() {
            
                @Override
                public void run() {
                
                    t1.start();
                    
                    while (t1.isAlive()) {
                        updateUI(compressor.getPercentDone());
                    }
                }
            
            });
            
            t2.start();
            
            return null;
        }
        
        // [...]
}

Solution

  • You could employee a producer/consumer pattern...

    Here's a really basic concept...

    public class ProducerComsumer {
    
        public static void main(String[] args) {
            new ProducerComsumer();
        }
    
        public ProducerComsumer() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
    
                    JPanel panel = new JPanel(new GridBagLayout());
                    panel.setBorder(new EmptyBorder(12, 12, 12, 12));
    
                    JProgressBar progressBar = new JProgressBar();
                    panel.add(progressBar);
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(panel);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    Producer producer = new Producer();
                    producer.start();
    
                    Consumer consumer = new Consumer(producer, progressBar);
                    consumer.start();
                }
            });
        }
    
        public class Producer extends Thread {
    
            private volatile float progress;
            private volatile boolean done;
    
            public Producer() {
                setPriority(NORM_PRIORITY - 1);
                setDaemon(true);
            }
    
            public float getProgress() {
                return progress;
            }
    
            public boolean isDone() {
                return done;
            }
    
            @Override
            public void run() {
                done = false;
                for (int index = 0; index < Integer.MAX_VALUE; index++) {
                    progress = (float) index / (float) Integer.MAX_VALUE;
                }
                done = true;
                System.out.println("All done...");
            }
        }
    
        public class Consumer extends Thread {
    
            private Producer producer;
            private JProgressBar progressBar;
    
            public Consumer(Producer producer, JProgressBar progressBar) {
                setDaemon(true);
                setPriority(NORM_PRIORITY - 1);
                this.producer = producer;
                this.progressBar = progressBar;
            }
    
            public JProgressBar getProgressBar() {
                return progressBar;
            }
    
            public Producer getProducer() {
                return producer;
            }
    
            @Override
            public void run() {
                while (!producer.isDone()) {
                    updateProgress();
                    try {
                        sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ProducerComsumer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                updateProgress();
            }
    
            protected void updateProgress() {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        int progress = Math.round(getProducer().getProgress() * 100f);
                        System.out.println("Update progress to " + progress);
                        getProgressBar().setValue(progress);
                    }
                });
            }
        }
    }
    

    Have a play around with the Thread.setPriority values and see if it makes any difference