Using a dual core processor using a single Java thread

Possible duplicate:
Was a multi-threaded Java application using a multi-core machine?

I have a simple and simple Java thread like this running on a dual-core computer (32-bit Windows XP)

public static void main(String[] strs) { long j = 0; for(long i = 0; i<Long.MAX_VALUE; i++) j++; System.out.println(j); } 

My assumption was that he would stick with a single processor in order to fully use the high-speed cache (since we continue to work with the local variable j in the loop, therefore, one processor usage will be 100% and the other will be largely idle. To my surprisingly, both processors are used approximately 40% ~ 60% after the start of the stream, and the use of one processor is slightly higher than the other.

My question is that there is any kind of OS load balancing mechanism that works when an imbalance is detected? In my case, is it possible that Windows found that one processor is almost 100% running and the other is almost idle, so it redirects the thread to another CPU periodically?

enter image description here

# EDIT1 I found a possible explanation: http://siber.cankaya.edu.tr/ozdogan/OperatingSystems/ceng328/node130.html

+7
java multithreading operating-system
source share
3 answers

When the OS executes threads, it starts each thread for a certain period of time (for example, 10-20 ms), then saves the state of the thread and looks for other threads to start.

Now, despite what you might think when looking at the processor usage graph, the OS actually runs a lot more threads than in your program. There are threads that run UI loops, threads that wait for I / O, threads that run background services, etc. Most threads spend most of their time blocking, waiting for something.

The reason I am talking about this is to explain that, from the point of view of the OS, the situation is more complicated than it might seem. There is a whole chain of threads performing a whole bunch of things, and the OS is trying to switch between them. Suppose you wanted to implement a heuristic that, if the thread last used all of its quantum, then the OS would do its best to schedule it in the same kernel. The OS needs to be tracked and taken into account more information, and the success of optimization may depend on many difficult to predict factors.

In addition, the advantage of thread affinity on the kernel is often almost negligible, so the OS does not try to do this automatically. Instead, they provide a function that allows the developer to explicitly indicate that a particular thread should be affinity to the kernel, and then the OS will respect the decision.

This seems like a reasonable compromise: if your thread works better when it is tied to the kernel, just ask the OS to do this. But the OS will not try to figure this out for you.

+3
source share

As you already mentioned, the OS will bypass threads around. The following native code also executes as you described.

 int main( int argc, char** argv ) { while( true ); return 0; } 

If you look at the process, it is constantly at 25% (using the quad core), but the resource monitor from Windows 7 shows that none of the 4 cores is at a constant 100%, although core 0 is at a higher level of use than others.

cpu can share the cache between the kernels, so this behavior does not mean that the cache is not used.

+1
source share

All Articles