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: 
A Thread is its own GC root. So any time you create a thread despite its creation context it will not be ready to GC until its run method completes. This is true even if the local method completes and the thread is still alive.

Example:

```
public void doSomeAsync(){
   Thread th = new Thread(new Runnable(){
      public void run(){
          Thread.sleep(500);
      }
   });
   th.start();
   //do something else quickly
}

```

After `//do somethign else quickly` anything defined that did not escape the method is then marked for GC. Thread th will not be marked for GC and is correctly placed on the heap with it's own thread-stack.
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).