SwingWorker在方法LockSupport.park()中等待很长时间

问题描述 投票:0回答:1

我正在Windows 10 64位和8 GB RAM上使用JDK 1.8。我有一个Swing应用程序。在应用程序中,我查询一个DB2数据库。该查询返回数据库表名称和每个表的查询条件的列表。这些表存在于单独的SQL Server Express数据库中。我的Swing应用程序为每个SQL Server Express数据库表创建一个单独的JComboBox,并使用接收到的条件查询该表,并使用查询结果填充JComboBox模型。我遍历DB2查询的结果,并为每一行创建一个新的JComboBox,并启动一个新的SwingWorker以查询SQL Server Express数据库。最初,我的应用程序在Windows 7上运行JDK 1.5。当然,SwingWorker不是1.5版JDK的一部分,因此我使用了第三方实现。原始文件工作正常,但是在Windows 10上迁移到JDK 1.8后,完成所有初始化过程要花费更多时间。使用VisualVM,时间的增加是由于SwingWorker线程在类park()的方法LockSupport中等待。我测量了执行该过程中每个步骤所花费的时间,其中大多数花费了百分之几秒才能完成,所有步骤的总时间不超过三秒钟。我尝试使用JDK 1.8版本的JDK 1.5应用程序中的SwingWorker实现,但是所花费的时间没有改变。如何发现导致SwingWorker线程在方法park()中花费约6秒的原因?或者,如何更改设计以避免出现此问题?

部分[伪]代码...

JPanel panel = new JPanel();
Connection db2Conn = // Connect to DB2
Statement s = db2Conn.createStatement();
ResultSet rs = s.executeQuery("SQL query");
while (rs.next()) {
    new ListTask(panel, /* data from 'rs' */).execute();
}

class ListTask extends SwingWorker<Void, Void> {
    // JComboBox will be added to this. See method 'done()'
    private JPanel panel;

    // Name of table in database.
    private String tableName;

    // Criteria for querying 'tableName'.
    private List<String> criteria;

    // Results of query.
    private Object[] results;

    public ListTask(JPanel aPanel, String table, List<String> where) {
        panel = aPanel;
        tableName = table;
        criteria = where;
    }

    protected void doInBackground() {
        // Populate "results"
        return null;
    }

    protected void done() {
        JComboBox<Object> combo = new JComboBox(results);
        panel.add(combo);
    }
}

VisualVM屏幕截图:

VisualVM Screen Capture

java swingworker
1个回答
0
投票
您的问题在其他地方。
© www.soinside.com 2019 - 2024. All rights reserved.