How to make a thread wait until a file is created in java?

A thread is interrupted when it is blocking (the call to sleep) and another thread calls its interrupt method. The call to interrupt must be made explicitly for this to happen.

A thread is interrupted when it is blocking (the call to sleep) and another thread calls its interrupt method. The call to interrupt must be made explicitly for this to happen. Seems that repeating the check for the file would be the logical thing to do if the thread is interrupted, but without knowing the cause of the interruption it's hard to say.As usual, when it comes to threading, Brian Goetz has something to say on the matter of InterruptedException: www-128.ibm.com/developerworks/java/libr....

I must agree Bombes comment: threads don't get interrupted on their own. Contrary to Jokis comment - they're not interrupted when a thread context swap takes place either (in fact, if a thread sleeps, it will surrender it's quantum to any thread that has work to do, but I digress). Furthermore, I would advise an alternative means of communication than polling for files.

You cannot be certain, for example, that once you have spotted a file, that it has been completely written without extra work from the file-writer (such as renaming it when ready, or creating a 'ready' file). Consider using something more 'data push' such as RMI, HTTP-POST, JMS queues, etc.

You should find out which thread interrupts that thread. Threads don’t do that on their own.

Please, can you tell me how I know which thread interrupts that thread – eman Dec 9 '08 at 12:27 Catch the exception and try to analyse the Exception object. There should be interesting information in it. – furtelwart Dec 9 '08 at 12:48.

If all you want is a notification when a file is created, AND you can (and willing) to go native (JNI) AND you want only win32 support, you could use the code here.

Yes, I need only to be notified when the file is created. I see the code attached but I don't understand what is the notify filter? Can you tell me please – eman Dec 10 '08 at 10:58 it's c++ code you're supposed to compile to a dll, then access via JNI (java native interface).

It's not platform independent and has other restrictions, and JNI is quite slow going through the interface, but it works perfectly. – Ran Biron Dec 15 '08 at 5:48 Link broken.... – Thorbjørn Ravn Andersen Sep 9 '10 at 13:41.

Well, if you don't know what InterruptedException is and/or don't want to do anything about it, obviously you should at least do something besides returning and exiting your loop. Take out the return, and then you'll keep waiting. But I'd check into why you're getting interrupted.

Something is trying to cancel your thread.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions