[讨论] 请问为什么Dalvik虚拟机在detach 当前线程的时候为什么要对Monitor还有frame做一些操作?求原因
pzz2011
2014-11-03
不知道JVM中是不是也有类似的动作呢!
求R大以及各位大神,好人的回复。 在此表示感谢了先! |
|
RednaxelaFX
2014-11-04
只有Java线程才可以执行Java代码。
JNI的 AttachCurrentThread() 机制允许一个当前不是Java线程的线程归入JVM的控制,成为一个Java线程。 而 DetachCurrentThread() 允许一个当前是Java线程的线程脱离JVM的控制。 当一个线程要detach了,它就即将不能执行任何Java代码了。自然,它也就不能持有任何Java层面的锁(monitor),所以在detach时要把该线程持有的monitor都释放掉。就这么简单。 其它清理动作也是同理。 楼主可以先读读JNI Invocation API的说明:http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html 然后再读Dalvik VM在代码里的注释: /* * Detach the thread from the various data structures, notify other threads * that are waiting to "join" it, and free up all heap-allocated storage. * * Used for all threads. * * When we get here the interpreted stack should be empty. The JNI 1.6 spec * requires us to enforce this for the DetachCurrentThread call, probably * because it also says that DetachCurrentThread causes all monitors * associated with the thread to be released. (Because the stack is empty, * we only have to worry about explicit JNI calls to MonitorEnter.) * * THOUGHT: * We might want to avoid freeing our internal Thread structure until the * associated Thread/VMThread objects get GCed. Our Thread is impossible to * get to once the thread shuts down, but there is a small possibility of * an operation starting in another thread before this thread halts, and * finishing much later (perhaps the thread got stalled by a weird OS bug). * We don't want something like Thread.isInterrupted() crawling through * freed storage. Can do with a Thread finalizer, or by creating a * dedicated ThreadObject class for java/lang/Thread and moving all of our * state into that. */ void dvmDetachCurrentThread() |
|
pzz2011
2014-11-04
有点困惑于R大说的“JAVA 线程" 我一直都没有做这种区分呀 可能因为我最近才开始看,所以还不清楚~
|
|
pzz2011
2014-11-04
还有就是注释中的栈 和java运行时栈指的是同一个意思吗?
|