简体   繁体   English

为什么有两个正在运行的线程而不是一个?

[英]Why there are two running threads instead of one?

So in the following: 所以在下面:

public class JavaClass {
    public static void main(String[] args) {
        JavaClass clazz = new JavaClass();
        clazz.startCustomThread();

        clazz = new JavaClass();
        clazz.startCustomThread();
    }

    public void startCustomThread() {
        new MyThread().startThread();
    }

    private static class MyThread {
        public void startThread() {
            new Thread(() -> {
                System.out.println("In thread " + Thread.currentThread().getName());
                while (true) {
                    try {
                        Thread.sleep(1000 * 5);
                        System.out.println("Woke up " + Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }).start();
        }
    }
}

The output is: 输出为:

In thread Thread-1
In thread Thread-0
Woke up Thread-0
Woke up Thread-1
....  

Since the clazz should be GC'd after the second instance and the first thread started in local scope of the first call to startCustomThread() 因为应该在第二个实例之后对clazz进行GC处理,并且第一个线程在对startCustomThread()的第一次调用的本地范围内启动
my question is why doesn't the first thread get terminated? 我的问题是为什么第一个线程不会终止?

You invoked startCustomThread method twice so you have started 2 Threads. 您两次调用了startCustomThread方法,因此您已经启动了2个线程。

From Thread docs : Thread文档:

The Java Virtual Machine continues to execute threads until either of the following occurs: Java虚拟机将继续执行线程,直到发生以下任何一种情况:

  1. The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. 已调用类Runtime的退出方法,并且安全管理器已允许进行退出操作。
  2. All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method. 不是守护程序线程的所有线程都已死,要么通过从调用返回到run方法,要么抛出传播到run方法之外的异常。

No of those options applies to your case so the thread is still alive. 这些选项均不适用于您的情况,因此线程仍处于活动状态。 This has nothing to do with garbage collection. 这与垃圾回收无关。

If you are asking specifically about why the JavaClass objcet is not GC'ed. 如果您要专门询问为什么不对JavaClass objcet进行GC。 it probably is (depending on VM parameters). 可能是这样(取决于VM参数)。

If you are asking why the thread spawned by that object continues to run, it is because it creates a new Thread and runs it. 如果您问为什么该对象产生的线程继续运行,那是因为它创建了一个新Thread并运行它。 This will only finish when either the run() method finished normally, or System.exit() is called. 仅当run()方法正常完成或调用System.exit()时,此操作才会完成。

The line clazz = new JavaClass(); clazz = new JavaClass(); is just assigning new object to your local reference and doesn't destruct previously created object this reference was referred to. 只是将新对象分配给您的本地引用,而不会破坏该引用所引用的先前创建的对象。 This line doesn't guarantee the object is garbage collected. 此行不能保证对象是垃圾回收。

Also object's garbage collection is not necessarily related to thread's lifecycle - your thread can be interrupted or finish its execution but object still may exist in the heap and being waited for GC. 同样,对象的垃圾回收不一定与线程的生命周期相关-您的线程可以被中断或完成其执行,但是对象仍可能存在于堆中并正在等待GC。

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

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