[英]When I run pg_restore via process in java application, application freeze
When I run pg_restore
via process in java application, application freeze and info message don't show, but process successfully finished. 当我通过Java应用程序中的进程运行
pg_restore
,应用程序冻结和信息消息未显示,但进程成功完成。 Why it so? 为什么会这样呢? I use PostgreSQL 9.2.
我使用PostgreSQL 9.2。
Method's code: 方法的代码:
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();
}
}
}
Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. 不要阻止EDT(事件调度线程)-发生这种情况时,GUI将“冻结”。 Instead of calling
Thread.sleep(n)
implement a Swing Timer
for repeating tasks or a SwingWorker
for long running tasks. 而不是调用
Thread.sleep(n)
实现用于重复任务的Swing Timer
或用于长时间运行的任务的SwingWorker
。 See Concurrency in Swing for more details. 有关更多详细信息,请参见Swing中的并发 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.