问题
In a multi-threaded Java application, if I want to identify the instructions where the context switch from one thread to another happens, what is the best approach? Instrumenting every instruction to identify what thread is executing the instruction will give a close approximation. For example, if the switch happens after the instrumentation is executed and before the actual instr. is executed, then the result will be imprecise.
Is there a way to precisely identify the context switch location (which will be different for different runs) in Java?
What is the most cost effective of identifying this?
回答1:
Short answer to your question is You can not do it in java
.It is the resposibility of JVM to switch the context based on underlying operating system and memory model.
http://en.wikipedia.org/wiki/Thread_(computing)
回答2:
Most context switching are caused by external interrupts (including timer). It can happen at any
instruction. Indeed, distribution is not even, but very random. Even if you could collect probability distribution of context switching for every instruction, how would you use it? Looks like you try to solve some problem with inadequate means. Better tell what your real problem is, and we try to suggest a solution.
来源:https://stackoverflow.com/questions/18964050/context-switching-in-java