Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:15:16+00:00 2026-06-12T09:15:16+00:00

I am running a very heavy process under an anonymous SwingWorker thread. In the

  • 0

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;
        }
        
        // [...]
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:15:17+00:00Added an answer on June 12, 2026 at 9:15 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running an algorithm at the moment that is very heavy but extremely parallel.
I'm running a very basic fetch request that returns about 2000 objects. I'm using
My wpf application was running very slow. I was using the performance profiling tools
I'm trying to speed up my code because it's running very long. I already
I am running a very simple program, which is trying to list files in
I'm running a very basic NSFetchRequest to fetch an entity MessageObject. I only have
I am interested in running a very long running rake task, one that would
I'm running into a very odd issue with a validator that is meant to
I am running into a very strange issue and I am convinced it is
So i am running into a very odd error. I am making a basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.