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
238 views
in Technique[技术] by (71.8m points)

java - Progress bar updater using up CPU

I want to keep my user informed of the progress of an I/O operation. At the moment I've got an inner class that I kick off before I start my I/O and stop after it's done. It looks like this:

class ProgressUpdater implements Runnable {

        private Thread thread;
        private long last = 0;
        private boolean update = true;
        private long size;

        public ProgressUpdater(long size) {
            this.size = size;
            thread = new Thread(this);
        }

        @Override
        public void run() {
            while (update) {
                if (position > last) {
                    last = position;
                    double progress = (double) position / (double) size * 100d;
                    parent.setProgress((int) progress);
                }
            }
        }

        public void start() {
            thread.start();
        }

        public void stop() {
            update = false;
            parent.setProgress(100);
        }
    }

parent is my reference to my UI and position is a field in my outer class that represents how far in the I/O we have progressed. I set progress to 100% when stopping because sometimes the I/O finishes and stops my updater before it can finish updating the previous increment. That just ensures it's at 100%.

At the moment, this works, and I use it like this:

ProgressUpdater updater = new ProgressUpdater(file.length());
updater.start();
//do I/O
//...
updater.stop();

The problem is that the loop eats CPU pretty badly. I tried throwing a lock (with a wait/notify) in there but I don't know what I'm doing when it comes to using wait/notify so it just hung my thread. What can I do to stop it from using so many CPU cycles?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You should look into using a SwingWorker. Basically you do all the IO on the background thread that the SwingWorker provides, and there are built-in ways of reporting the progress to the swing thread.

Then whenever you update that position, you can automatically update the progress, rather than polling continuously, which is what you've done so far.

Take a look at Swingworker Timeout or SwingWorker with FileReader to see if either will help.

An alternative solution, if you don't want to use a SwingWorker, would probably just be to, instead of updating that position value, update the progress bar directly, with a call like this:

SwingUtilities.invokeLater(new Runnable() {
    @Override public void run() {
        getMyProgressBar().setValue(position);
    }
});

Assuming you've set up the progress bar so that its max is the size of the file.


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

...