【问题标题】:Speeding up Java application加速 Java 应用程序
【发布时间】:2012-12-14 21:12:18
【问题描述】:

我的应用程序侦听某个目录及其子目录。对于收听目录,我使用JNotify。在目录应用程序上创建新文件时,会检查文件并以某种方式对其进行处理。下面是代码:

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

    public class JNotifyDemo {

    public void sample() throws Exception {
        // path to watch
        //String path = System.getProperty("user.home");
        String path = "/folder";
        System.out.println(path);

        // watch mask, specify events you care about,
        // or JNotify.FILE_ANY for all events.
        int mask = JNotify.FILE_CREATED
                | JNotify.FILE_DELETED
                | JNotify.FILE_MODIFIED
                | JNotify.FILE_RENAMED;

        // watch subtree?
        boolean watchSubtree = true;

        // add actual watch
        int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());

        // sleep a little, the application will exit if you
        // don't (watching is asynchronous), depending on your
        // application, this may not be required
        Thread.sleep(1000000);

        // to remove watch the watch
        boolean res = JNotify.removeWatch(watchID);
        if (!res) {
            // invalid watch ID specified.
        }
    }

    class Listener implements JNotifyListener {

        public void fileRenamed(int wd, String rootPath, String oldName,
                String newName) {
            print("renamed " + rootPath + " : " + oldName + " -> " + newName);
        }

        public void fileModified(int wd, String rootPath, String name) {
            print("modified " + rootPath + " : " + name);
        }

        public void fileDeleted(int wd, String rootPath, String name) {
            print("deleted " + rootPath + " : " + name);
        }

        public void fileCreated(int wd, String rootPath, String name) {
            print("created " + rootPath + " : " + name);
            //check file whether it is xml or not
            //validate xml
            //do some internal processing of file
            // and do other jobs like inserting into database
        }

        void print(String msg) {
            System.err.println(msg);
        }
    }

    public static void main(String[] args) throws Exception {
        new JNotifyDemo().sample();
    }
}

从代码中可以看出,应用程序每次处理一个文件。有什么建议可以加快这个应用程序的速度,比如使用线程或其他任何东西?

【问题讨论】:

  • 什么需要加速?在知道是否应该优化以及应该优化什么之前不要优化。
  • 我在这里看不到任何瓶颈,因为你没有对你的代码做任何事情。它只是演示中的一个观察者代码。
  • 可能是synchronized print?然后是延迟消息队列。

标签: java multithreading


【解决方案1】:

我建议您使用扩展Watchable 接口的java.nio.file.Path,它可以注册到监视服务,以便监视更改和事件。

这种事件驱动的方法可以加速您的应用程序。看看example

PathWatchable 都包含在 java 7 中。

【讨论】:

【解决方案2】:

实际进行了多少处理?请记住,IO 非常慢,除非您的处理需要相当长的时间,否则多线程解决方案在读取文件时会遇到瓶颈。

无论如何,实现并行处理的快速方法是启动一个ExecutorService 并提交一个Runnable 并将文件路径作为参数。

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多