简体   繁体   中英

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

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

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 Thread object representing the thread won't keep the underlying OS thread alive.

一旦run方法完成它的执行,你不需要,线程退出

Generally cleaning up is done by the garbage collector. If the threads uses files/sockets you may need to close them. The best practice is to close resources in the top-level finally block in Thread::run .

Actually, you need to clean up your data, and not the thread.

Nopes. The thread would execute and die on its own and get garbage collected.

No its not necessary. When the thread exit its run method, the thread come into exit state itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM