首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在javax.swing.Timer中分离任务?

如何在javax.swing.Timer中分离任务?
EN

Stack Overflow用户
提问于 2015-10-26 01:21:18
回答 1查看 32关注 0票数 1

为了完成三项任务,我正在编写一个程序:

  1. 使用ftp将xml文件从远程文件夹下载到本地文件夹。
  2. 阅读和处理这些文件。
  3. 从本地文件夹中删除文件。

我使用javax.swing.Timer周期性地重复了这一点。

我希望阻止任务2从开始直到任务一完成,并阻止任务3从开始直到任务2完成。

我怎样才能做到这一点

坦克斯

这是我的代码:

代码语言:javascript
复制
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();   
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-26 01:39:36

简单地说,您可以使用单个线程执行程序来完成请求,例如.

代码语言:javascript
复制
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的UI (我找不到任何证据)。

然后,您可以简单地使用java.util.Timer来安排Executor上的下载请求,在队列中的所有其他任务完成之前不会执行该请求。

代码语言:javascript
复制
Timer timer = new Timer("Download Timer");
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        executor.execute(new DownloadRunnable());
    }
}, 0, TIMER_DELAY);

其中DownloadRunnable只是Runnable实现的一个类

代码语言:javascript
复制
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();
        }

    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33337096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档