Is garbage collector a daemon thread?

2019-03-18 14:21发布

问题:

Is garbage collector a daemon (background) thread?

Thanks.

回答1:

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.



回答2:

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



回答3:

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)



回答4:

on jdk 1.8 following threads are listed with

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

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



回答5:

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.

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