Link Java Thread Terminates Links

Why is it not possible to stop the thread by setting a null reference and allowing the garbage collector? This is an object like any other, right?

Example:


Thread t = new Thread(new Runnable() {
  public void run() {
    //...
  } 
}).start;

t = null;

+5
source share
4 answers

JLS section 12.6 says the following:

"A reachable object is any object that can be accessed in any potential ongoing computation from any live thread."

, , . Thread , ( @Jon Skeet) Thread, Thread.currentThread() .

+2

, , ?

, . . , , , , ?

, , Thread , , , ( ) Thread, , .

+3

JFrame null, . JVM , , , JVM . , , .

+2

null.

, , - Thread. :

System.out.println(Thread.currentThread());

What do you expect from this if the object Threadwas garbage collected?

Objects are just garbage collected at some point after there are no live links to them. Any live stream has a link to itself.

Honestly, it would be painful if you were sure that you saved the link you just started to prevent its sudden termination.

+2
source

All Articles