Understanding the Reference Handler thread

耗尽温柔 提交于 2019-12-20 17:01:04

问题


I am continuing my path to deep understanding of Java Thread. Unfortunately my Java Certification didn't cover that part, so the only way of learning is to post a series of dumb questions. With so many years of Java Development, I am sometimes wondering how much I still have to learn :-)

In particular my attention is now with the reference handler thread.

"Reference Handler" daemon prio=10 tid=0x02da3400 nid=0xb98 in Object.wait() [0x0302f000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x1aac0320> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:485)
    at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
    - locked <0x1aac0320> (a java.lang.ref.Reference$Lock)

Now some questions are following, for some of them I know the answer, but I am not posting it, because I would like to hear someone else opinions:

  1. What is the Reference Handler thread supposed to do ?
  2. A thread dump should be considered bottom up, why does the stack trace start with locked, shouldn't the lock statement appears at least after the thread has run ?
  3. What does "Native Method" means ?
  4. Why "Unknown Source", in which case the thread dump cannot recall the source code ?
  5. Lastly the waiting on and locked has the same , why ?

as usual, I kindly ask to answer all the questions, so that I can mark answered.


回答1:


  1. I suspect it handles running finalizers for the JVM. It's an implementation detail and as such not specified in the JVM spec.
  2. This only means that the java.lang.ref.Reference$Lock was locked in the method mentioned in the line preceding it (i.e in ReferenceHandler.run().
  3. "Native Method" simply means that the method is implemented in native (i.e. non-Java) code (think JNI).
  4. Unknown Source only means that the .class file doesn't contain any source code location information (at least for this specific point). This can happen either when the method is a synthetic one (doesn't look like it here), or the class was compiled without debug information.
  5. When a thread waits on some object, then it must have locked that object at some point down the call trace, so you can't really have a waiting on without a corresponding locked.



回答2:


1) The Finalizer Thread calls finalizer methods. The Reference Thread has a similar purpose.

http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Core/lang/java/lang/ref/Reference.java.htm

The OpenJDK source states its is a

High-priority thread to enqueue pending References

The GC creates a simple linked list of references which need to be processed and this thread quickly adds them to a proper queue. The reason this is done in two phases is that the GC does nothing but find the References, this thread calls code which handles those references e.g. Call Cleaners, and notifies ReferenceQueue listeners.

2) A lock is acquired for a synchronized method before it is entered.

3-5) covered by Joachim ;)




回答3:


Wow, too deep for me. I can only answer one or two of your questions.

"Native Method" simply means the implementation of that method is in some native (i.e. C or C++) library. Once the call stack has "gone native", the JVM can no longer monitor it. No way for it to provide additional stack information.

"Unknown Source" likely means the code was compiled with optimization turned on and debugging info turned off (-g flag?). This eliminates the file/line information from the .class file.



来源:https://stackoverflow.com/questions/7658670/understanding-the-reference-handler-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!