簡體   English   中英

Java SwingWorker並發問題

[英]Java SwingWorker Concurrency Issue

我有一個執行文件操作的Java程序,並且我有一個具有JTextArea的GUI類,該JTextArea的控制台輸出已重定向到該類。 我正在嘗試讓SwingWorker在另一個類中發布到該JTextArea,但是我似乎無法使其正常工作。 在我的GUI類中,我具有以下方法:

public ShopUpdaterGUI() {
    initComponents();
    redirectSystemStreams();
}

private void updateTextArea(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            consoleTextAreaInner.append(text);
          }
        });
}


private void redirectSystemStreams() {
      OutputStream out = new OutputStream() {
        @Override
        public void write(int b) throws IOException {
          updateTextArea(String.valueOf((char) b));
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
          updateTextArea(new String(b, off, len));
        }

        @Override
        public void write(byte[] b) throws IOException {
          write(b, 0, b.length);
        }
      };

      System.setOut(new PrintStream(out, true));
      System.setErr(new PrintStream(out, true));
    }    

然后在我做這件事的另一堂課中,我有以下內容:

public void update(){
    (updateTask = new UpdateTask()).execute();
}

private class UpdateTask extends SwingWorker<Void, String>{

        @Override
        protected Void doInBackground() {
            try{
                publish("Creating backup...");
                mainBackup.createBackup();
                publish("Setting restore point...");
                setRestorePoint();
                publish("Applying update...");
                UPDHandler.unZipUpdate();
                saveModifiedFilesList();

            }catch(IOException ex){
                ex.printStackTrace();
            }catch(ClassNotFoundException ex){
                ex.printStackTrace();
            }finally{
                publish("Update Complete!");
            }

            return null;
        }


}

編輯:這是我的處理方法:

    protected void process(List<String> updates){
        String update = updates.get(updates.size() - 1);
        System.out.println(update);
    }

有時這可以工作,但有時卻完全跳過了publish()調用之一。 例如,當到達publish(“ Setting restore point ...”)時,它將永遠不會真正顯示在JTextArea上,而只是跳至“ Applying Update ...”。

當我告訴它時,如何准確地發布和更新JTextArea?

doInBackground()內部使用publish(V... chunks)將數據發送到process(List<V> chunks)以在GUI線程中對其進行處理。 您錯過的是覆蓋處理方法:

@Override
 protected void process(List<String> chunks) {
     for (String s : chunks) {
         textArea.append(s + "\n");
     }
 }

請勿發布或處理任何內容。 在您的情況下,由於JTextArea正在接收從System.out重定向的輸出,因此您似乎要做的就是在需要在JTextArea中顯示的文本上調用System.out.println(...)

private class UpdateTask extends SwingWorker<Void, String>{

    @Override
    protected Void doInBackground() {
        try{
            publish("Creating backup...");
            mainBackup.createBackup();

            // publish("Setting restore point...");
            System.out.println("Setting restore point...");

            setRestorePoint();

            // publish("Applying update...");
            System.out.println("Applying update...");

            UPDHandler.unZipUpdate();
            saveModifiedFilesList();

        }catch(IOException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }finally{
            publish("Update Complete!");
        }

        return null;
    }
}

有關包含后台線程使用的更完整的示例,請參見此答案中的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM