Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my method wait for a file to exist before continuing

Tags:

java

There is an external program that creates an XML file, but it may take a while to create. I need my Java program to wait until the file exists before moving on.

I've been reading up a little bit about synchronized blocks, and I read that I can do something like this:

synchronized(this) {
    while (!file.exists) { this.wait(); }
}

To be honest I don't really know much about synchronized tasks so I'm wondering if I'm on the right track, or if I'm way off.

like image 289
namxal Avatar asked Apr 06 '15 14:04

namxal


1 Answers

A typical way to solve this problem is for your XML writer to create the XML file, and when it it is done, it should create a second file saying the work is done.

Your java program should listen for the existence of the .done file rather than the XML file.

Won't work if you don't have any control over the XML writing application, though.

like image 56
user1717259 Avatar answered Oct 05 '22 22:10

user1717259