我正在编写一个程序,以完成3个任务:
我使用javax.swing.Timer定期重复。</ p>
我想阻止任务2开始,直到任务1完成,并阻止任务3开始直到任务2完成。
我怎样才能实现
thnks
这是我的代码:
new javax.swing.Timer(TIMER_DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
FTPFileDownloader ftpdown = new FTPFileDownloader();
try {
ftpdown.downloadFiles(Configuration.array.get(0), Configuration.array.get(3), Configuration.array.get(4), Configuration.array.get(5), Configuration.array.get(6));
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
XmlToDB xmldb=new XmlToDB();
ReadFilesFromFolder readfiles=new ReadFilesFromFolder();
File file=new File("C:\\FTP_CLIENT_DIRECTORY\\");
ArrayList<File> output=readfiles.listFilesForFolder(file, true, "");
ArrayList<String> ss = null;
try {
ss = xmldb.XMLtoString(output);
int nbrechang=xmldb.insert(ss);
} catch (IOException | SAXException | ParserConfigurationException E) {
// TODO Auto-generated catch block
E.printStackTrace();
}
try {
FileUtils.cleanDirectory(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}).start();
答案 0 :(得分:4)
很简单,您可以使用单线程执行程序来暂存请求,例如......
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
@Override
public void run() {
FTPFileDownloader ftpdown = new FTPFileDownloader();
try {
ftpdown.downloadFiles(Configuration.array.get(0), Configuration.array.get(3), Configuration.array.get(4), Configuration.array.get(5), Configuration.array.get(6));
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
XmlToDB xmldb = new XmlToDB();
ReadFilesFromFolder readfiles = new ReadFilesFromFolder();
File file = new File("C:\\FTP_CLIENT_DIRECTORY\\");
ArrayList<File> output = readfiles.listFilesForFolder(file, true, "");
ArrayList<String> ss = null;
try {
ss = xmldb.XMLtoString(output);
int nbrechang = xmldb.insert(ss);
} catch (IOException | SAXException | ParserConfigurationException E) {
// TODO Auto-generated catch block
E.printStackTrace();
}
try {
FileUtils.cleanDirectory(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
这假设您没有更新基于Swing的用户界面(我无法找到任何证据)
然后,您可以简单地使用java.util.Timer
在Executor
上安排下载请求,该请求将被执行,直到队列中的所有其他先前任务完成为止
Timer timer = new Timer("Download Timer");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
executor.execute(new DownloadRunnable());
}
}, 0, TIMER_DELAY);
DownloadRunnable
只是Runnable
实施的一类
public class DownloadRunnable implements Runnable {
@Override
public void run() {
FTPFileDownloader ftpdown = new FTPFileDownloader();
try {
ftpdown.downloadFiles(Configuration.array.get(0), Configuration.array.get(3), Configuration.array.get(4), Configuration.array.get(5), Configuration.array.get(6));
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
XmlToDB xmldb = new XmlToDB();
ReadFilesFromFolder readfiles = new ReadFilesFromFolder();
File file = new File("C:\\FTP_CLIENT_DIRECTORY\\");
ArrayList<File> output = readfiles.listFilesForFolder(file, true, "");
ArrayList<String> ss = null;
try {
ss = xmldb.XMLtoString(output);
int nbrechang = xmldb.insert(ss);
} catch (IOException | SAXException | ParserConfigurationException E) {
// TODO Auto-generated catch block
E.printStackTrace();
}
try {
FileUtils.cleanDirectory(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}