
Signature
Knute Johnson
email s/nospam/knute/
smartnhandsome wrote:
>> I want to use TimerTask to watch for a file in a particular folder.
>> and proceed to another line of code only after i get the file in a
>> particular folder.
> import java.io.*;
> import java.util.*;
[quoted text clipped - 14 lines]
> }
> }
Now, finding out if that file not only exists but is complete, that's another
issue. You could find the file and another process might still be writing to
it.

Signature
Lew
Knute Johnson - 01 Nov 2007 00:56 GMT
> smartnhandsome wrote:
>>> I want to use TimerTask to watch for a file in a particular folder.
[quoted text clipped - 23 lines]
> another issue. You could find the file and another process might still
> be writing to it.
See the "//do your thing" comment above :-).

Signature
Knute Johnson
email s/nospam/knute/
Martin Gregorie - 01 Nov 2007 01:49 GMT
> Now, finding out if that file not only exists but is complete, that's
> another issue. You could find the file and another process might still
> be writing to it.
Shouldn't be a problem provided that the writer completes the file,
closes it and then renames it to something that the reader is expecting.
We used that approach in a production system where files were delivered
by FTP and renamed when the PUT operation completed. It worked fine: we
had no problems with incomplete files.

Signature
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
smartnhandsome - 01 Nov 2007 04:51 GMT
Thanks every one for there replies, but the code still works like the
same way as I had written, when we create a TimerTask and override the
run method and pass this to timer it creates a new thread and then
pass back the control to the main thread, so the main thread still
keeps doing next steps while the timer thread that was created earlier
would run back ground. I wanted my code (main thread ) to wait till
timer task thread finishes its file watch. How can this be achieved??
Thanks again for every ones posts.
Knute Johnson - 01 Nov 2007 05:47 GMT
> Thanks every one for there replies, but the code still works like the
> same way as I had written, when we create a TimerTask and override the
[quoted text clipped - 5 lines]
>
> Thanks again for every ones posts.
do {
File f = new File("????");
if (f.exists())
break;
try {
Thread.sleep(1000);
} catch (InterruptedException ie) { }
} while (true) ;
But if the file never shows up your program never runs again. I would
use this instead of a TimerTask, it is inline and simpler if you want to
halt your program until the file arrives.
If you insist on a TimerTask;
use a boolean flag and a wait object
boolean flag;
final Object o = new Object();
// main thread
synchronized (o) {
while (!flag)
o.wait();
flag = false;
}
// TimerTask...
if (f.exists())
synchronized (o) {
flag = true;
o.notify();
}

Signature
Knute Johnson
email s/nospam/knute/
smartnhandsome - 02 Nov 2007 04:40 GMT
Knute
Thanks for your post i tried using your suggestion on synchronized
block with TimerTask but i was getting Illegal State Exception. I did
not understand why you were passing Object o into the block?.But your
simple inline sleep code was working fine. Thanks again.
Knute Johnson - 02 Nov 2007 04:59 GMT
> Knute
> Thanks for your post i tried using your suggestion on synchronized
> block with TimerTask but i was getting Illegal State Exception. I did
> not understand why you were passing Object o into the block?.But your
> simple inline sleep code was working fine. Thanks again.
I'm a firm believer in the KISS method.
The Object o is just so that you can get a lock from a common object.
The lock is used to synchronize the actions. Thread interactions are
very complex and a science into themselves.

Signature
Knute Johnson
email s/nospam/knute/
Lew - 02 Nov 2007 05:14 GMT
smartnhandsome wrote:
>> Thanks for your post i [sic] tried using your suggestion on synchronized
>> block with TimerTask but i [sic] was getting Illegal State Exception.
Did you mean IllegalStateException?

Signature
Lew
Daniel Pitts - 02 Nov 2007 02:09 GMT
> smartnhandsome wrote:
>>> I want to use TimerTask to watch for a file in a particular folder.
[quoted text clipped - 23 lines]
> another issue. You could find the file and another process might still
> be writing to it.
Generally, most OSes have a way to rename a file as an atomic operation.
Doing this allows you to create the file under a different name, fill
it appropriately, and then rename it to what its supposed to. Barring
that, you should obtain a lock on the file (not sure Java supports that).

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>