java 垃圾收集器是守护线程吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5218848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:01:59  来源:igfitidea点击:

Is garbage collector a daemon thread?

javamultithreadinggarbage-collection

提问by bunty

Is garbage collector a daemon (background) thread?

垃圾收集器是守护进程(后台)线程吗?

Thanks.

谢谢。

采纳答案by Buhake Sindi

I will assume yes, Garbage collector thread is a daemon thread. Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation or other requests for the java runtime system.

我会假设是的,垃圾收集器线程是一个守护线程。守护线程是一个低优先级的线程,它在后台间歇性地运行,为 java 运行时系统执行垃圾收集操作或其他请求。

回答by adarshr

It's not a thread from a java.lang.Threadperspective at least.

java.lang.Thread至少从一个角度来看,这不是一个线程。

回答by TT_

Yes: http://www.javaperspective.com/daemon-threads.html: (Daemon threads are considered as threads that run in the background and they are generally used as service providers for user threads. For example, the Java garbage collector is a daemon thread)

是的:http: //www.javaperspective.com/daemon-threads.html:(守护线程被认为是在后台运行的线程,它们通常用作用户线程的服务提供者。例如,Java垃圾收集器是一个守护线程)

回答by bina ramani

on jdk 1.8 following threads are listed with

在 jdk 1.8 上列出了以下线程

ThreadMXBean mxbean = ManagementFactory.getThreadMXBean();
    for(long id:mxbean.getAllThreadIds())
        System.out.println(mxbean.getThreadInfo(id));

Output -

输出 -

  1. "Attach Listener" Id=5 RUNNABLE
  2. "Signal Dispatcher" Id=4 RUNNABLE
  3. "Finalizer" Id=3 WAITING on java.lang.ref.ReferenceQueue$Lock@63947c6b
  4. "Reference Handler" Id=2 WAITING on java.lang.ref.Reference$Lock@2b193f2d
  5. "main" Id=1 RUNNABLE
  1. “附加侦听器”Id=5 RUNNABLE
  2. “信号调度器”Id=4 RUNNABLE
  3. java.lang.ref.ReferenceQueue$Lock@63947c6b 上的“终结器”Id=3 WAITING
  4. java.lang.ref.Reference$Lock@2b193f2d 上的“参考处理程序”Id=2 WAITING
  5. “主要”Id = 1 可运行

There is no GC thread. It can safely be said garbage collection process is native.

没有 GC 线程。可以肯定地说垃圾收集过程是原生的。

回答by srajan

A daemon thread is also a thread that continues to run even after the JVM exits. From Oracle documentation When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: ?The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. ?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.

守护线程也是一个即使在 JVM 退出后仍继续运行的线程。来自 Oracle 文档 当 Java 虚拟机启动时,通常有一个非守护进程线程(通常调用某个指定类的名为 main 的方法)。Java 虚拟机继续执行线程,直到发生以下任一情况: ? Runtime 类的退出方法已被调用,并且安全管理器已允许退出操作发生。? 所有不是守护线程的线程都已死亡,无论是从对 run 方法的调用返回,还是通过抛出传播到 run 方法之外的异常。

So if GC is a daemon thread, it should be a native thread spawned by the java run time, but can continue to run after he JVM exits

所以如果GC是守护线程,应该是java运行时产生的原生线程,但是JVM退出后可以继续运行