开发者

Is the Timer the best to use for a file dispatcher?

I'm working on a program to move files from one folder to another. I have about 1000 files and each file must be moved at a fixed hour. Right now I'm using a Timer like:

Timer timer  = new Timer();
    for (int k = 0; k < f开发者_StackOverflow中文版ileList.size(); k++) {
            FileObj fileObj = fileList.get(k);




            Calendar date = Calendar.getInstance();
            date.add(Calendar.SECOND, fileObj.getTimeToLaunch());

            CopyTask  copyTask = new CopyTask();
            copyTask.setTaskName("fileName");


            timer.schedule(
                    copyTask,
                    date.getTime(),
                    1000 * 60 * 60 * 24 * 7
            );

            }

As I can see right now I have only one thread that will do the job. The problem is that each file must me moved at a precised time so if the previous file is longer to be moved, the next one will be moved later. I'm thinking about creating one timer per file but I don't know if the host will support so many threads.

I would like to know what will be the best choice ? Or maybe another solution ? A middle solution maybe ?


Timer is very simple solution which works very well for small timing needs. But as you've identified, each scheduled execution is relative to the previous one which doesn't sound ideal for your situation.

I'd recommend having a look at the widely used open source quartz scheduler.


Whether you use quartz or your own solution, your environment will have to support enough threads to copy a large number of files at once. How many files will you need to move simultaneously?


You can use ScheduledExecutorService, as it will handle multiple threads. There is an example on how to use it on the API docs.

It is not as powerful as Quartz, but it is quite simple and adds no external dependencies.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜