I need to read the data from a table every minute through thread & then perform certain action.
Should I just start a thread & put it in sleep mode for 1 minute, once the task is done. And then again check if the table has any data, perform the task again & go to sleep for 1 minute…
Is this the right approach? Can any one provide me some sample code in Java for doing the same?
Thanks!
As so often, the Java 5 extensions from the java.util.concurrent package are a huge help here.
You should use the ScheduledThreadPoolExecutor. Here is a small (untestet) example:
Update:
A benefit of using an Executor is you can add many repeating tasks of various intervals all sharing the same threadpool an the simple, but controlled shutdown.