进度条更新程序使用CPU [英] Progress bar updater using up CPU

查看:111
本文介绍了进度条更新程序使用CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望让用户了解I / O操作的进度。目前我有一个内部课程,我在开始I / O之前启动并在完成后停止。它看起来像这样:

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 是我对UI的引用, position 是我的外部类中的一个字段,表示我们在I / O中取得的进展。我在停止时将进度设置为100%,因为有时I / O完成并停止我的更新程序,然后才能完成更新前一个增量。这只是确保它是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();

问题在于循环使CPU非常糟糕。我尝试在那里抛出一个锁(带有等待/通知),但我不知道在使用wait / notify时我正在做什么,所以它只是挂了我的线程。我该怎么做才能阻止它使用这么多CPU周期?

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?

推荐答案

你应该考虑使用 SwingWorker 。基本上你在 SwingWorker 提供的后台线程上执行所有IO,并且有内置的方法向swing线程报告进度。

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.

查看 Swingworker超时使用FileReader 进行工作,看看是否有任何帮助。

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

另一种解决方案,如果你不想使用 SwingWorker ,可能只是,而不是更新位置值,更新直接进度条,调用如下:

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.

这篇关于进度条更新程序使用CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆