Searching thousands of files via a thread and “tree walker”, writing matches to JTable results in unresponsiveness

六月ゝ 毕业季﹏ 提交于 2020-01-06 04:20:06

问题


I have revised my Windows 7 search program to write matched files to a JTable. Prior to this I was writing to a JTextArea. The output was ugly; hence JTable output. But with JTable, the program becomes unresponsive from time to time if searching thousands of files. (Not so with JTextArea. Very smooth.)

My question is how to improve responsiveness.

I have one Thread that is invoked in main and "walks the file tree" from a given node:

  public static void main(String args[]) {
      EventQueue.invokeLater(new Runnable() {
        public void run() {
          gui = new GUI();
          Utilities.disable(GUI.btnStop);
        }});   
     t = new Thread(new TASK());
     taskStarted = false;
  }
}

Here's the class header for TASK:

public class TASK extends SimpleFileVisitor<Path> implements Runnable{

Here's run:

  public void run() 
  {
      SearchyGUI.disposition = FileVisitResult.CONTINUE;
      Files.walkFileTree(path , this);
  }

Here's the main routine for tree walking (there are 3 others, one for processing directories, one for post-processing them, and one for errors):

  public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException {
    if(a.isSymbolicLink())
      return SearchyGUI.disposition;
    File file = new File(f.getParent().toString(), f.getFileName().toString());
    try 
    {
      if(f.getFileName().toString().toLowerCase().matches(fPatt.toLowerCase().trim())
          report(s);
    } catch (MalformedURLException | SAXException | TikaException ex) {msgbox(ex.toString());}
    return SearchyGUI.disposition;
  }

Matched files and their info are written to a JTable:

      private static void report(...){
        ArrayList<String> rowData = new ArrayList<>(4);
              rowData.add(date);
              rowData.add(size);
              rowData.add(filename);    
              rowData.add(path);
              dftTableModel.addRow(new Object[]{rowData.get(0),rowData.get(1),rowData.get(2),rowData.get(3)});
          }
          catch (Exception e)
          { 
            SearchyGUI.disposition = FileVisitResult.TERMINATE;
          }
      }

To improve responsiveness, I don't know if I need another Thread for writing to the JTable or if I need to use a SwingWorker. In either case, I'm not sure how to go about implementation.

Assuming another Thread, would I turn the report method into a class that implements Runnable? I'm having trouble visualizing implementation but I can probably figure it out if I have to.

I once used SwingWorker, probably incorrectly (with no Thread), with this program. I gave it up when the output area flashed incessantly. So I'm not keen on going that route, but I will figure it out if advisable.

I don't think the question falls in the "opinion" area. Need advice on how to improve responsiveness. I just don't want to head down a wrong path if someone can point the error in doing so in advance.

来源:https://stackoverflow.com/questions/32040032/searching-thousands-of-files-via-a-thread-and-tree-walker-writing-matches-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!