Java线程垃圾收集与否 [英] Java Thread Garbage collected or not

查看:80
本文介绍了Java线程垃圾收集与否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题发布在某个网站上。我没有找到正确的答案,所以我在这里再次发布。

  public class TestThread {
public static void main(String [] s){
// anonymous类扩展Thread
Thread t = new Thread(){
public void run(){
//无限循环
while(true){
try {
Thread.sleep(1000);
} catch(InterruptedException e){
}
//只要这行打印出来,就知道它是活着的。
System.out.println(线程正在运行...);
}
}
};
t.start(); //行A
t = null; //线B
//没有更多线程的引用t
//另一个无限循环
while(true){
try {
Thread.sleep(3000) ;
} catch(InterruptedException e){
}
System.gc();
System.out.println(Executed System.gc());
} //程序将一直运行,直到您使用^ C来停止它为止


code $ pre

我的查询不是关于停止线程。让我重述一下我的问题。
Line A(见上面的代码)启动一个新的Thread;和B行使线程引用为空。因此,JVM现在有一个线程对象(它处于运行状态),其中没有引用存在(如线B中的t = null)。
所以我的问题是,为什么这个线程(主线程中没有引用)继续运行,直到主线程运行。根据我的理解,线程对象应该在B行后面被垃圾回收。我试着运行这段代码5分钟以上,请Java运行时运行GC,但线程不停止。



希望这两次代码和问题都是明确的。

解决方案

正在运行的线程被认为是所谓的垃圾收集根,是防止收集垃圾的东西之一。当垃圾收集器确定您的对象是否为 reachable ',它总是使用垃圾收集器根作为参考点。



考虑一下,为什么你的主线程没有被垃圾回收,没有人引用那个。


This question was posted on some site. I didnt find right answers there, so I am posting it here again.

public class TestThread {
    public static void main(String[] s) {
        // anonymous class extends Thread
        Thread t = new Thread() {
            public void run() {
                // infinite loop
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    // as long as this line printed out, you know it is alive.
                    System.out.println("thread is running...");
                }
            }
        };
        t.start(); // Line A
        t = null; // Line B
        // no more references for Thread t
        // another infinite loop
        while (true) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            System.gc();
            System.out.println("Executed System.gc()");
        } // The program will run forever until you use ^C to stop it
    }
}

My query is not about stopping a Thread. Let me rephrase my question. Line A(see code above) starts a new Thread; and Line B make the thread reference null. So, the JVM now has a Thread Object(which is in running state) to which no reference exists (as t=null in Line B). So my question is, why does this thread(that has no reference anymore in the main thread) keeps on running until the main thread is running. Per my understanding, the thread object should have been garbage collected post Line B. I tried to run this code for 5 minutes and more, requesting Java Runtime to run GC, but the thread just does not stop.

Hope both the code and question is clear this time.

解决方案

A running thread is considered a so called garbage collection root and is one of those things keeping stuff from being garbage collected. When the garbage collector determines whether your object is 'reachable' or not, it is always doing so using the set of garbage collector roots as reference points.

Consider this, why is your main thread not being garbage collected, no one is referencing that one either.

这篇关于Java线程垃圾收集与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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