Java中的上下文切换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18964050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:56:37  来源:igfitidea点击:

Context switching in Java

javamultithreadingscheduling

提问by user1539577

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.

在多线程 Java 应用程序中,如果我想确定上下文从一个线程切换到另一个线程的指令,最好的方法是什么?检测每条指令以识别正在执行该指令的线程将给出近似值。例如,如果切换发生在检测执行之后和实际指令之前。被执行,那么结果将是不精确的。

  • 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?

  • 有没有办法在 Java 中精确识别上下文切换位置(不同的运行会有所不同)?

  • 识别这个最经济有效的方法是什么?

回答by amicngh

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.

对您的问题的简短回答是。You can not do it in java根据底层操作系统和内存模型切换上下文是 JVM 的责任。

http://en.wikipedia.org/wiki/Thread_(computing)

http://en.wikipedia.org/wiki/Thread_(计算)

回答by Alexei Kaigorodov

Most context switching are caused by external interrupts (including timer). It can happen at anyinstruction. 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.

大多数上下文切换是由外部中断(包括定时器)引起的。它可能发生在any指令。事实上,分布不是均匀的,而是非常随机的。即使你可以为每条指令收集上下文切换的概率分布,你会如何使用它?看起来你试图用不充分的手段解决一些问题。最好告诉您真正的问题是什么,我们会尝试提出解决方案。