I have been assigned on designing a java program on a linux machine that:
- Connects to a database
- reads a record
- retrieve certain information and send to Nagios according to a field known as ‘threat_level’
- read next record and repeat step number 3 until all records have been read
Now, I needed to get this to run every few minutes; so what my partner did was create a script that uses a loop to run the program, sleeps a few minutes, and repeat.
Recently, my boss told us that its good but would like the whole procedure to be completely self contained in java; meaning that it loops and sleeps within java. On top of that, he would like to have the sleep duration be determined by command line each time that the program is run.
I did some research and it seems that using Thread.sleep() is inefficient in certain circumstances and I cannot tell if this is one of them or not. Also, I am still unclear on how to have the sleep time be determined via command line upon running the program. I can provide the code if necessary.
Thread.sleep()is just fine, especially when you want to sleep for “few minutes“:Thread.sleep()might be inefficient or not precise enough in millisecond time ranges, but not in your case. But if you want the process to run in the same frequency (as opposed to with fixed delay), consider:This is important of “do your job” part might take significant amount of time and you want the process with exact frequency.
Also for readability consider
TimeUnitclass (usesThread.sleep()underneath):