7

In the below example, new Thread() doesnt have any reference. 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 ?

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();
    }
}
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
randeepsp
  • 3,572
  • 9
  • 33
  • 40

3 Answers3

13

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". It won't be garbage collected until (after) it finishes.

In the below example, new Thread() doesnt have any reference. 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.

Also without extending Thread class or implementing runnable, how are we creating a thread?

In your example, you have created anonymous subclass of Thread; i.e. a class that extends Thread.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • also to his question on how the thread is created you should add that he actually does create a subclass of `Thread` albeit an anonymous subclass. – PeterT Jun 20 '11 at 09:37
  • Also note that any threads that are running will also stop the JVM from shutting down. – Tim Sparg Jun 20 '11 at 09:39
  • if the start method is called by some other code after more than a month or two , will the Thread be garbage collected ? – randeepsp Jun 20 '11 at 09:51
  • 2
    @randeepsp, Time is not important. A second is about 3,000,000,000 clock cycles (i.e. an eternity) for a computer. It will behave the same after one year as it does after one second. – Peter Lawrey Jun 20 '11 at 10:28
3

No, a Thread which has been can't be garbage collected before the underlying VM thread (whether an OS-thread or not) has finished. 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.

As for your second question - your code does extend Thread, using an anonymous inner class, here:

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:

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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.

In answer to your second question, you are extending the Thread class by implementing your own run() function

Bart Vangeneugden
  • 3,436
  • 4
  • 33
  • 52