开发者

optimize this java code?

开发者 https://www.devze.com 2023-02-27 09:36 出处:网络
I have a quartz scheduler job which scans a directory every 10 seconds and convert all pcl files to pdf files.

I have a quartz scheduler job which scans a directory every 10 seconds and convert all pcl files to pdf files.

Here I put part of the code which scans the directory and loads all the files and for each of it it calls some methods to convert it.

    FileExtensionFilter extFilter = new FileExtensionFilter();
    extFilter.setAllowAll(false);
    extFilter.addExtension(_fExtension);
    // Filter out all files that match.
    File[] filteredFileList = fInbox.listFiles(extFilter);
    for (File fSrc : filteredFileList) {
       try {
          //call the methods to convert fSrc file
       }catch (Exception e){
         //threat exception's code
       }

All works nice if I have 30 pcl files but if I have 50 for instance, the processing goes step by step slower.

The question is: is there any way to improve this loop? Basically to not "wait" for t开发者_运维技巧he file to be processed but go further and take another one and so one?

I'm thinking about multithreading but I am not sure if this will work on this filesystem scan conversion task... Can you suggest something?

Thanks.

Ps: Please note that If I throw 30 files, and after that another 30 all works great. The performance is affected when I throw at once more files...


Throttle it so it only picks 20 files to do each 10 seconds. If you throw 50 files at it, it will do 20, and remove those, then do 20 more, then remove those, then do 10 more and remove those.

It may take a little longer (some downtime) but you won't screw up your scheduler.


I'd suggest taking a look at jNotify. It will allow you to get notified at the exact time when a file is appears/is modified/deleted in a directory. This way, you can start processing when the file appears, and this could be better than processing all files at once.

This will be a part of JDK core in Java 7. This article will tell you more about it. The java.nio.file package has a WatchService API to support this.

Although, as others have said, your problem is more I/O bound than CPU bound. If you're doing a lot of conversions, a faster hard drive, possibly a SSD, will be of greater help.

0

精彩评论

暂无评论...
验证码 换一张
取 消