从内存中删除Java中的线程时? [英] When thread in Java is removed from memory?

查看:81
本文介绍了从内存中删除Java中的线程时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Java API文档:

From the Java API doc:

Java虚拟机将继续执行线程,直到执行以下操作 发生:

The Java Virtual Machine continues to execute threads until following occurs:

不是守护程序线程的所有线程都已死亡,或者通过返回 从对run方法的调用或引发异常 传播到run方法之外.

All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

我希望我的假设是正确的,一旦线程完成其run()方法,它就有资格进行垃圾回收.在相同的情况下,我只是想知道:

I hope my assumption is correct that once thread finishes its run() method it becomes eligible for garbage collection. In the same context I am just curious to know about:

  1. 如果返回后仍不符合垃圾收集的条件 从run()中,是否应该将其引用设置为null?
  2. 符合垃圾收集条件并不一定意味着 该对象将从内存中删除.这是全权酌情决定的 底层操作系统/JVM进行垃圾回收时. 但是如何才能(通过Java程序或外部工具)确保从中完全删除了对象 记忆吗?
  3. 如果说线程在完成其run()方法后就死了,那为什么 我仍然可以在isAlive()getState()上执行 同一线程对象?这两个调用都返回falseRUNNABLE 分别.
  1. If it doesn't become eligible for garbage collection after returning from run(), should one set its reference to null to do that?
  2. Being eligible for garbage collection doesn't necessarily mean that the object will be removed from memory. It is at the sole discretion of the underlying operating system / JVM when it is garbage collected. But how can one make sure (either through a Java program or external tool) that the object is completely removed from the memory?
  3. If a thread is said to be dead once it finishes its run() method, why can I still be able to execute isAlive() or getState() on the same thread object? Both the calls return false and RUNNABLE respectively.

推荐答案

Thread类是本机内存中真实线程的代理.

The Thread class is a proxy for the real thread which is in native memory.

我希望我的假设是正确的,即一旦线程完成其run()方法,它就有资格进行垃圾回收.

I hope my assumption is correct that once thread finishes its run() method it becomes eligible for garbage collection.

run()之后实际上有一些代码,该代码处理未捕获的异常.

There is actually a bit of code after run(), this code handles uncaught exceptions.

线程死后,无需GC就可以立即释放其本机内存和堆栈.但是,Thread对象与其他任何对象一样,并且一直存在,直到GC决定它可以是免费的为止.没有足够的参考.

Once the thread dies its native memory and stack are freed immediately without needing a GC. However, the Thread object is like any other object and it lives until it is GC has decided it can be free e.g. there is no strong reference to it.

类似地,FileOutputStream是操作系统中文件的代理.即使文件已被close()甚至删除,您仍然可以引用该对象.

Similarly, a FileOutputStream is a proxy for a file in the operating system. You can still have a reference to the object even after the file has been close() or even deleted.

如果从run()返回后仍不符合垃圾回收的条件,是否应该将其引用设置为null呢?

If it doesn't become eligible for garbage collection after returning from run(), should one set its reference to null to do that?

您几乎不需要在任何地方执行此操作.实际上,通常最简单的方法是不首先保留对该线程的引用,或者不使用ExecutorService来管理您的线程.

You rarely need to do this anywhere. In fact it is often simpler not keep a reference to the thread in the first place, or to use an ExecutorService to manage your threads.

当我有一个具有Thread字段的对象时,当线程死亡时,我经常使该对象死亡,因此不需要将该字段null删除.

When I have an object which has a Thread field I often have this object die when the thread dies, thus the field doesn't need to be nulled out.

我还使用了用于Fork/Join的内置线程池.这是一种在后台线程中执行任务的更轻巧的方法,因为它不会创建和破坏线程太多.

I also use the built in thread pool used for Fork/Join. This is a more light weight way to perform tasks in a background thread as it doesn't create and destroy threads so much.

ExecutorService fjp = ForkJoinPool.commonPool();

符合垃圾回收的条件并不一定意味着该对象将从内存中删除.进行垃圾回收时,由底层操作系统/JVM自行决定.但是,如何确保(通过Java程序或外部工具)将对象从内存中完全删除?

Being eligible for garbage collection doesn't necessarily mean that the object will be removed from memory. It is at the sole discretion of the underlying operating system / JVM when it is garbage collected. But how can one make sure (either through a Java program or external tool) that the object is completely removed from the memory?

您不能,而且一般不应该尝试. GC将在需要时清理资源.

You can't and generally shouldn't try. The GC will clean up resources when it needs to.

如果说一个线程在完成其run()方法后就死了,为什么我仍然可以在同一个线程对象上执行isAlive()或getState()?这两个调用分别返回false和RUNNABLE.

If a thread is said to be dead once it finishes its run() method, why can I still be able to execute isAlive() or getState() on the same thread object? Both the calls return false and RUNNABLE respectively.

线程对象与其他任何对象一样.只要持有对它的引用,就可以在其上调用方法.

The thread object is like any other object. You can call methods on it for as long as you hold a reference to it.

这篇关于从内存中删除Java中的线程时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆