Grant
Grant

Reputation: 4543

Java how to delay a program execution

I have four web applications uses four different files to record logs. From my main application (log analyzer) I execute those four applications and then I'm going to read log files generated, but there is a lag (about 5sec and depends on machine execution speed) in between application execution and log generation.

What I want to do is some how wait the log analyzer execution till all these four log files are created and updated. Now I'm doing this by using

Thread.sleep(6000)

but this system fails on some environments which has low execution rate. I want to get an idea about how to implement a better solution for this by continuously checking whether the four files are created and content has been created.

Upvotes: 0

Views: 222

Answers (2)

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19837

Your log analyzer should wait for the log generation, not actively poll. Use join:

The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,

t.join();

causes the current thread to pause execution until t's thread terminates.

Upvotes: 0

npinti
npinti

Reputation: 52185

Instead of waiting, you could use a file system watcher to watch a directory and take action when a specific event has been fired.

This would allow your applications to not wait unnecessarily or else not to take action too early, which is what happens when you provide a fixed time period.

Upvotes: 1

Related Questions