当我在java应用程序中通过进程运行pg_restore
时,应用程序冻结和信息消息不显示,但是处理成功完成。为什么会这样?我使用PostgreSQL 9.2。
方法代码:
private void restoreDB() {
JFileChooser fileopen = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("DB FILES", "sql");
fileopen.addChoosableFileFilter(filter);
fileopen.setCurrentDirectory(new java.io.File("C:/backups/"));
fileopen.setDialogTitle("Выберите папку для резервной копии");
fileopen.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileopen.setAcceptAllFileFilterUsed(false);
int ret = fileopen.showDialog(null, "Restore DB");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
try {
String path = file.getAbsolutePath();
System.out.println(path);
String user = "postgres";
String dbName = "Auto";
String executeCmd = "pg_restore -i -U " + user + " -d " + dbName+" -v "+ path;
Process runtimeProcess;
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
javax.swing.JOptionPane.showMessageDialog(null, "Successfull!");
log.info("Successfull!");
} else {
javax.swing.JOptionPane.showMessageDialog(null, "Unsuccessfully");
log.info("Unsuccessfully");
}
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
答案 0 :(得分:3)
不要阻止EDT(事件调度线程) - 当发生这种情况时,GUI将“冻结”。而不是调用Thread.sleep(n)
为重复任务实现Swing Timer
或为长时间运行的任务实现SwingWorker
。有关详细信息,请参阅Concurrency in Swing。