Is garbage collector a daemon thread?

2019-03-18 14:36发布

Is garbage collector a daemon (background) thread?

Thanks.

5条回答
Deceive 欺骗
2楼-- · 2019-03-18 14:41

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

查看更多
Fickle 薄情
3楼-- · 2019-03-18 14:54

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.

查看更多
可以哭但决不认输i
4楼-- · 2019-03-18 14:54

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-18 14:57

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)

查看更多
对你真心纯属浪费
6楼-- · 2019-03-18 15:02

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.

查看更多
登录 后发表回答