How do I cleanup a Thread after its run method finishes?

2022-09-01 20:01:36

What should be done with a Thread after its method finishes executing? Is there any cleanup needed for a Thread in Java?run()


答案 1

Unless the thread's work has used some unmanaged resources (network streams, files etc) - in which case it should clean up after itself - there's nothing you need to do.

Note that holding a reference to the object representing the thread won't keep the underlying OS thread alive.Thread


答案 2

You don't need to, thread exits , once method finishes it's executionrun