简体   繁体   English

当没有引用的新Thread()将被垃圾收集

[英]When wil the new Thread() without reference be garbage collected

In the below example, new Thread() doesnt have any reference. 在下面的示例中,新的Thread()没有任何引用。 Is it possible that it be garbage collected below it is dead ? 是否有可能是垃圾收集在它下面死了? Also without extending Thread class or implementing runnable, how are we creating a thread ? 也没有扩展Thread类或实现runnable,我们如何创建一个线程?

public class TestFive {
    private int x;
    public void foo() {
            int current = x;
            x = current + 1;
    }
    public void go() {
            for(int i = 0; i < 5; i++) {
                    new Thread() {
                            public void run() {
                                    foo();
                                    System.out.print(x + ", ");
                            } 
                    }.start();
            } 
    }
    public static void main(String args[]){
            TestFive bb = new TestFive();
            bb.go();
    }
}

A new thread that has not been started will be garbage collected when it becomes unreachable in the normal way. 一个尚未启动的新线程将以正常方式无法访问时进行垃圾回收。

A new thread that has been started becomes a garbage collection "root". 已启动的新线程将成为垃圾收集“root”。 It won't be garbage collected until (after) it finishes. 它完成之前(之后)不会被垃圾收集。

In the below example, new Thread() doesnt have any reference. 在下面的示例中,新的Thread()没有任何引用。 Is it possible that it be garbage collected below it is dead ? 是否有可能是垃圾收集在它下面死了?

No. It has been started, and hence won't be garbage collected until it finishes / dies. 不,它已经启动,因此在完成/死亡之前不会被垃圾收集。 And it does have a reachable reference until (at least) the point at which the start() call returns. 确实有一个可达的引用,直到(至少) start()调用返回的点。

Also without extending Thread class or implementing runnable, how are we creating a thread? 也没有扩展Thread类或实现runnable,我们如何创建一个线程?

In your example, you have created anonymous subclass of Thread ; 在您的示例中,您创建了Thread匿名子类; ie a class that extends Thread . 即一个扩展Thread的类。

No, a Thread which has been can't be garbage collected before the underlying VM thread (whether an OS-thread or not) has finished. 不,一个Thread它已经不能被垃圾底层VM线程(操作系统线程与否是否)完成之前收集的。 I'm not sure offhand whether a Thread which doesn't have any obvious references to it but hasn't been start() -ed ends up causing a leak, or whether it can be collected - the latter, I'd expect. 我不确定是否一个没有任何明显引用它但没有start() -ed的Thread最终导致泄漏,或者它是否可以被收集 - 后者,我期望。

As for your second question - your code does extend Thread , using an anonymous inner class, here: 关于你提到的第二个问题-你的代码确实扩展 Thread ,使用匿名内部类,在这里:

new Thread() {
        public void run() {
                foo();
                System.out.print(x + ", ");
        } 
}

I would personally suggest that even if you did want to use an anonymous inner class here, it's generally neater to implement Runnable : 我个人建议,即使你确实想在这里使用一个匿名内部类,它通常更适合实现Runnable

new Thread(new Runnable() {
    public void run() {
        foo();
        System.out.print(x + ", ");
    } 
})

That way it's clear that you're just providing something to run, rather than trying to change any of the thread's core behaviour. 这样很明显,你只是提供一些东西来运行,而不是试图改变任何线程的核心行为。

Your thread will be available for garbage collection once it has finished running. 一旦运行完毕,您的线程将可用于垃圾回收。 The end of your for loop does not influence it, as the for loop runs in a different thread. for循环的结束不会影响它,因为for循环在不同的线程中运行。

In answer to your second question, you are extending the Thread class by implementing your own run() function 在回答第二个问题时,您通过实现自己的run()函数扩展Thread类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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