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/
世界丰富多彩,知识天花乱坠。
---如果有帮到你,点个赞吧~
原文地址:https://www.cnblogs.com/java2sap/p/15132791.html