English 中文(简体)
SwingWorker: CPU 100%的使用!
原标题:SwingWorker: CPU usage 100%!

当一些数据与外部来源脱钩时,我的航程申请有一席之地。 用SwaingWorker的透镜显示数据Im。

 // SwingWorker Thread
 private class DisplayRowsTask extends SwingWorker<Void, Object[]> {

    private Vector<String[]> _data;

    @Override
    protected Void doInBackground() {
        while (_continue && !hasError()) {
            // some logic to fetch and place data in _data
            // data adjustment
            for (String[] row : _data) publish (row);
            _data = new Vector<String[]>();
        }
        return null;
    }

    @Override
    protected void process(List<Object[]> rows) {
        for (Object[] row : rows) ((DefaultTableModel)_table.getModel()).addRow(row);
    }
}

所展示的成果是正确的,而且一切都做了细微的工作,因为邮联的使用率是100%(而且超过!),特别是在很多数据经常被搁置的情况下。 我如何限制这种使用?

另一个问题可能不完全相关:我如何知道数据是否在表格中显示? “工人”方法没有提供这种信息

问题回答

Your CPU is going up to 100% naturally, because you re idly looping while (!task.isDone()); in run(). SwingWorker is executed asynchronously by calling execute(); so you don t have to execute it in a Thread. You shouldn t use Thread anyway in Swing. The SwingWorker does the job. It has hook methods to invoke code when it s finished. If you have many tasks to execute implement them in the SwingWorker itself.

与此类似:

   @Override
    protected Void doInBackground() {
      while (_continue && !hasError()) {
        // do some adjustments around _data
        for (String[] row : _data) publish (row);
       }
       return null;
    }

To another question: I think addRow(...) fires event. Your table should get this event and invalidate component. If you want to execute code after SwingWorker is done, then override done() method.

不能回答。

由于我无法通过执行,使加拿大邮联的利用率达到100%,因此我并不认为你尝试了什么。 如果你是民主选举学会的用户,那么你可以把自己的代码用在JProfiler上,那么你就可以看到目前的国际竞争主管机构的所有重要事件,尽管你基本上有三项工作。

(1) 执行 Executor & SwingWorker , note about take

2)通过将你的方法总结到,可宣布的#Thread,向全球调查组发出的通知产出必须列入invoke Later,

3) 创建一些,翻新或执行《执行》,对《全球倡议》的通知输出必须归纳为invoke Later,在某些情况下,需要列入invokeAndWait

4) 您可以模拟/配合履行问题:Christmas 树木应用





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签