• SwingWorker.publish 方法注释写的很清楚, 尤其是标红处


    void javax.swing.SwingWorker.publish(V... chunks)

    Sends data chunks to the process method. This method is to be used from inside the doInBackground method to deliver intermediate results for processing on the Event Dispatch Thread inside the process method.

    Because the process method is invoked asynchronously on the Event Dispatch Thread multiple invocations to the publish method might occur before the process method is executed. For performance purposes all these invocations are coalesced into one invocation with concatenated arguments.

    For example:

     publish("1");
     publish("2", "3");
     publish("4", "5", "6");
     

    might result in:

     process("1", "2", "3", "4", "5", "6")
     

    Sample Usage. This code snippet loads some tabular data and updates DefaultTableModel with it. Note that it safe to mutate the tableModel from inside the process method because it is invoked on the Event Dispatch Thread.

     class TableSwingWorker extends 
             SwingWorker<DefaultTableModel, Object[]> {
         private final DefaultTableModel tableModel;
     
         public TableSwingWorker(DefaultTableModel tableModel) {
             this.tableModel = tableModel;
         }
     
         @Override
         protected DefaultTableModel doInBackground() throws Exception {
             for (Object[] row = loadData(); 
                      ! isCancelled() && row != null; 
                      row = loadData()) {
                 publish((Object[]) row);
             }
             return tableModel;
         }
     
         @Override
         protected void process(List<Object[]> chunks) {
             for (Object[] row : chunks) {
                 tableModel.addRow(row);
             }
         }
     }
     
    Parameters:
    chunks intermediate results to process
    See Also:
    process
    博客地址: https://www.cnblogs.com/java2sap/
    世界丰富多彩,知识天花乱坠。
    ---如果有帮到你,点个赞吧~
  • 相关阅读:
    Nginx 之 Nginx配置实例——负载均衡
    Nginx 之 Nginx配置实例——反向代理
    OverFeat:Integrated Recognition, Localization and Detection using Convolutional Networks
    Fourier Transform
    Pytest中使用pytest.exit中断用例执行
    Pytest自定义测试报告
    idea spring源码踩坑日记
    数据结构笔记八:排序
    数据结构笔记七:查找
    数据结构笔记六:图
  • 原文地址:https://www.cnblogs.com/java2sap/p/15132791.html
Copyright © 2020-2023  润新知