実行中にインターフェイスを完全にロックする IOBound タスクを含む SwingWorker スレッドがあります。通常のワークロードをカウンター ループに交換しても、結果は同じです。SwingWorker は基本的に次のようになります。
public class BackupWorker extends SwingWorker<String, String> {
private static String uname = null;
private static String pass = null;
private static String filename = null;
static String status = null;
BackupWorker (String uname, String pass, String filename) {
this.uname = uname;
this.pass = pass;
this.filename = filename;
}
@Override
protected String doInBackground() throws Exception {
BackupObject bak = newBackupObject(uname,pass,filename);
return "Done!";
}
}
それを開始するコードは、JFrame を拡張するクラスに存在します。
public void actionPerformed(ActionEvent event) {
String cmd = event.getActionCommand();
if (BACKUP.equals(cmd)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final StatusFrame statusFrame = new StatusFrame();
statusFrame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run () {
statusFrame.beginBackup(uname,pass,filename);
}
});
}
});
}
}
StatusFrame の興味深い部分は次のとおりです。
public void beginBackup(final String uname, final String pass, final String filename) {
worker = new BackupWorker(uname, pass, filename);
worker.execute();
try {
System.out.println(worker.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
私が見る限り、「長時間実行」はすべてワーカーによって処理され、EDT の GUI に触れるものはすべて処理されます。それとも、SwingWorker に期待しすぎているのでしょうか?