Is there anyway to figure out the java timeslice on windows?
I know that the jvm timeslices threads,开发者_开发知识库 at least on windows. Is there anyway I can find out what the default timeslice is, or change the timeslice?
One way I thought about is to write my own round robin scheduler and run it as a thread at a high priority level, and have it control all other threads. Would that work?
In fact, unless you are using a JVM that uses "green threads", it is the operating system that ultimately does thread time-slicing and thread scheduling. If you need to modify the scheduling, etc behaviour, start by looking at the facilities for doing this provided by your operating system.
One way I thought about is to write my own round robin scheduler and run it as a thread at a high priority level, and have it control all other threads. Would that work?
There are no guarantees. It depends on how responsive the OS'es thread scheduler is to changes in thread priorities.
Other problems with this approach (adjusting thread priorities) are:
- this will inevitably make your application more platform specific with respect to the interactions between your scheduler and the real (black box) thread scheduler,
- it will be really hard to know if your scheduler is working effectively, and
- it will make tracking down "heisenbugs" in your application harder.
Why do you think that it is necessary to do this?
To answer the first part of your question.
I think the following test can estimate time slice. General idea: start N threads (N > Runtime.getRuntime().availableProcessors() ) running in the following loop:
public void run() {
long warmUp = 20000;
long milli0 = System.currentTimeMillis();
long nano0 = System.nanoTime();
while (true) {
long milli1 = System.currentTimeMillis();
long nano1 = System.nanoTime();
if (warmUp > 0) {
warmUp--;
} else {
if (nano1 < nano0) {
log("WARNING: Nanotime goes back by " + (nano1 - nano0) + " ns");
warmUp = 20000;
} else
if (nano1 - nano0 > SPIKE) {
log("WARNING: Nanotime gap: " + (nano1 - nano0)/MILLIS_PER_NANO + " ms. System time delta: " + (milli1 - milli0) + " ms.");
warmUp = 20000;
}
}
nano0 = nano1;
milli0 = milli1;
}
}
Result will very much depends on platform. On my Windows 2008 Server I am getting 5-15 milliseconds.
"The term "quantum" is a unitless measure of time for each time slice that a thread will run until a "context switch" occurs and another thread (either within the same program or from within another program) is selected to run. This prevents a CPU-bound process from monopolizing the processor. Currently in Windows, 3 quantums are equal to either 10 milliseconds (single processor) or 15 milliseconds (multiple-processor Pentium). This depends on the hardware abstraction layer (HAL) selected for your computer. Original Equipment Manufacturer (OEM) HALs may have a different value. Time slices that are fixed at 36 quantums are currently used when background services are selected (as you might choose in a typical server installation)."
精彩评论