单核Java线程运行时的双核CPU利用率 [英] Dual-core CPU utilization w/ single Java thread running

查看:138
本文介绍了单核Java线程运行时的双核CPU利用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

解决方案

当OS执行线程时,它会运行每个线程一段时间(比如10-20ms),然后保存线程的状态,并查找其他线程运行。



现在,尽管您在查看CPU利用率图表时可能会想到,操作系统实际上运行的线程比程序中的线程多得多。有线程运行UI循环,线程在I / O上等待,线程运行后台服务等等。大多数线程花费大部分时间阻止等待某些东西。



我之所以谈论这个,是因为从操作系统的角度解释,情况比它看起来更复杂。有很多线程在做很多事情,操作系统试图在它们之间切换。假设您想要实现一种启发式方法,如果一个线程最后一次耗尽其整个量程,那么操作系统将努力将其安排到同一个核心。操作系统需要跟踪并考虑更多信息,优化的成功可能取决于许多难以预测的因素。



此外,将线程关联到核心的好处在实践中通常可以忽略不计,因此操作系统不会尝试自动执行此操作。相反,他们公开了一个功能,允许开发人员明确表示特定线程应该与核心关联,然后操作系统将尊重该决定。



比如一个合理的权衡:如果你的线程在关注核心时表现更好,那就请求操作系统这样做。但是,操作系统不会费心试图为您解决这个问题。


Possible Duplicate:
Would a multithreaded Java application exploit a multi-core machine very well?

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

public static void main(String[] strs) {

    long j  = 0;
    for(long i = 0; i<Long.MAX_VALUE; i++)
        j++;

    System.out.println(j);
    }

My expectation was that it would stick to a single CPU to fully exploit the high-speed cache(since in the loop we keep operating with local variable j, hence one CPU utiliaztion would be 100% and the other would be pretty much idle. To my suprise both of the CPUs are being utilized at around 40%~60% after the thread starts and the utilization of one CPU is slightly higher than the other.

My question is that Is there any OS load-balancing mechanism that kicks in when out-of-balance has been detected? In my case is it possible that Windows OS found that one CPU is hitting nearly 100% and the other is almost idle so it reschedules the thread to another CPU periodically?

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

解决方案

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

Now, despite what you might think from looking at the CPU Utilization graph, the OS is actually running a lot more threads than those from your program. There are threads running UI loops, threads waiting on I/O, threads running background services, etc. Most of the threads spend most of their time blocked waiting on something.

The reason why I'm talking about this is to explain that from OS's point of view, the situation is more complex than it might look. There are a whole bunch of threads doing a whole bunch of things, and the OS is attempting to switch between them. Suppose that you wanted to implement a heuristic that if a thread used up its entire quantum the last time, then the OS will make an effort to schedule it to the same core. The OS needs to track and take into account more information, and the success of the optimization may depend on a lot of hard-to-predict factors.

In addition, the benefit of affinitizing a thread to a core are often negligible in practice, so OSes don't attempt to do it automatically. Instead, they expose a feature that allows the developer to explicitly say that a particular thread should be affinitized to a core, and then the OS will respect the decision.

That seems like a reasonable trade-off: if your thread performs better when affinitized to a core, just ask the OS to do that. But, the OS won't bother attempting to figure it out for you.

这篇关于单核Java线程运行时的双核CPU利用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆