Java SwingWorker创建多个线程

Java SwingWorker创建多个线程,java,multithreading,swing,swingworker,Java,Multithreading,Swing,Swingworker,我不知道我是否正确使用了SwingWorker。在我的需求中,我需要每秒钟刷新一次,所以我需要一个SwingWorker,它需要每秒钟执行一次 下面是我的代码的一部分,说明了我是如何循环我的SwingWorker的 // Parent class /** * Initialize background services. */ private void initServices() { this.dtModel = (DefaultTableModel) tblHistor

我不知道我是否正确使用了SwingWorker。在我的需求中,我需要每秒钟刷新一次,所以我需要一个SwingWorker,它需要每秒钟执行一次

下面是我的代码的一部分,说明了我是如何循环我的SwingWorker的

// Parent class

/**
 * Initialize background services.
 */
private void initServices() {
        this.dtModel = (DefaultTableModel) tblHistory.getModel(); // Default data model of the jTable
        this.updateTimer = new Timer(1000, new ActionListener() { // I don't know if this is right, I use Timer to loop the Swingworker
            @Override
        public void actionPerformed(ActionEvent e) {
                    new CallHistoryWatcherService(CallHistory.this, updateTimer, dtModel).execute();
                }
        });
        updateTimer.setRepeats(false);
    updateTimer.start();
}

// Watcher class

/**
 * Watch the CallInformations if any update has been made.
 * This will check every seconds.
 * @param history
 * @param updateTimer
 * @param dtModel 
 */
public CallHistoryWatcherService(CallHistory history, javax.swing.Timer updateTimer, DefaultTableModel dtModel) {
    // Initialize values
}

@Override
protected Collection<Vector> doInBackground() throws Exception {
        Collection<Vector> chunks = new ArrayList<>();
        for (CallInformations info : CallInformations.getCallInformations(OrderBy.Ascending)) {
        Vector v = new Vector();
                // Get from data from Database then place it to Vector
                chunks.add(v);
        }
        return chunks;
}

@Override
protected void done() {
        try {
        history.updateTableData(get()); // From parent class, manually update the data based on new Collection of data
        System.out.println("Call History refreshed...");
        } catch (ExecutionException | InterruptedException e) { 
        e.printStackTrace();
        }
        COLUMN_NAMES.clear();
        updateTimer.restart(); // Again, I don't know if this is right, restart the timer to execute the SwingWorker
}
//父类
/**
*初始化后台服务。
*/
私有服务(){
this.dtModel=(DefaultTableModel)tblHistory.getModel();//jTable的默认数据模型
this.updateTimer=new Timer(1000,new ActionListener(){//我不知道这是否正确,我使用Timer循环Swingworker
@凌驾
已执行的公共无效操作(操作事件e){
新的CallHistoryWatcherService(CallHistory.this、updateTimer、dtModel).execute();
}
});
setRepeats(false);
updateTimer.start();
}
//观察者类
/**
*如果有任何更新,请查看CallInformation。
*这将每秒钟检查一次。
*@param历史
*@param-updateTimer
*@param-dtModel
*/
公共CallHistoryWatcherService(CallHistory历史,javax.swing.Timer UpdateTime,DefaultTableModel dtModel){
//初始化值
}
@凌驾
受保护的集合doInBackground()引发异常{
集合块=新的ArrayList();
for(CallInformations信息:CallInformations.getCallInformations(OrderBy.升序)){
向量v=新向量();
//从数据库中获取数据,然后将其放入Vector
块。添加(v);
}
返回块;
}
@凌驾
受保护的void done(){
试一试{
history.updateTableData(get());//从父类中,根据新的数据集合手动更新数据
System.out.println(“刷新呼叫历史记录…”);
}catch(ExecutionException | interruptedeexception e){
e、 printStackTrace();
}
列名称。清除();
updateTimer.restart();//同样,我不知道这是否正确,请重新启动计时器以执行SwingWorker
}

如以下文件的Javadoc所述:

SwingWorker的生命周期涉及三个线程:

  • 当前线程
  • 工作线程
  • 事件调度线程
后台作业在单个辅助线程上完成。但是
SwingWorker
的内部实现通过
ExecutorService
使用共享线程池来运行多个
SwingWorker
的后台作业。引用自
SwingWorker.execute()的Javadoc:

安排此SwingWorker在工作线程上执行。有个工作线程可用

因此,您看到的是非常正常的,因为这些是
SwingWorker
内部使用的线程池中的线程

编辑:


在你的情况下,我不会使用
SwingWorker
。我只需要使用一个单独的线程(而不是EDT)来完成这项工作,完成后,我将更新UI,但在EDT中,调用
SwingUtilities.invokeLater()
。我会使用一个可重复的
计时器
定期执行这项工作。

我已经编辑了我的问题和上面说明的代码。