Question: I am wondering what happen if you declare a local thread within a method? Normally all the local variables will be gone as soon as the function returns since they are all allocated on Stack. However, it seems that a local thread would be a different story. Is that right? 

```
public int A() {
    Thread t = new Thread() {
        doSomething();
    }
    t.start();
    return -1;
 }

```

Answer: 
If the variable is of a primitive, then it'll be on the stack and will be gone when the method returns -- but your thread's `Runnable` instance (or whatever contains the meat of the thread) will have a copy of that primitive value.

If the variable is of a reference type, then the object is allocated on the heap and lives until there are no more references to it, at which point it's eligible for garbage collection. The *reference* to that object is on the stack and will be gone when the method returns, but as with primitives, the thread's `Runnable` will have a copy of that same reference (and will thus keep that object alive).
If you spawn a local `Thread` within a method, only the local method variables declared as `final` will stick around until the `Thread` has completed. When the `Thread` completes its `run()` method, the thread and any final variables it had available to it from the method that created it will get garbage collected like everything else.

**Clarification**

Only `final` variables used within the original method AND the spawned Thread's `run()` method will refrain from being garbage collected until both the method and the `run()` method completes. If the thread doesn't access the variable, then the presence of the thread will not prevent the variable from being garbage collected after the original method completes.

**References**

<http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html>