In my Java application I have a Runnable such as:
this.runner = new Runnable({
@Override
public void run() {
// do something that takes roughly 5 seconds.
}
});
I need to run this roughly every 30 seconds (although this can vary) in a separate thread. The nature of the code is such that I can run it and forget about it (whether it succeeds or fails). I do this as follows as a single line of code in my application:
(new Thread(this.runner)).start()
Now, this works fine. However, I'm wondering if there is any sort of cleanup I should be doing on each of the thread instances after they finish running? I am doing CPU profiling of this application in VisualVM
and I can see that, over the course of 1 hour runtime, a lot of threads are being created. Is this concern valid or is everything OK?
N.B. The reason I start a new Thread
instead of simply defining this.runner
as a Thread
, is that I sometimes need to run this.runner
twice simultaneously (before the first run call has finished), and I can't do that if I defined this.runner
as a Thread
since a single Thread
object can only be run again once the initial execution has finished.