开发者

How to force two Java threads to run on same processor/core?

I would like a solution that doesn't include critical sections or similar synchronization alternati开发者_运维百科ves. I'm looking for something similar the equivalent of Fiber (user level threads) from Windows.


The OS manages what threads are processed on what core. You will need to assign the threads to a single core in the OS.

For instance. On windows, open task manager, go to the processes tab and right click on the java processes... then assign them to a specific core.

That is the best you are going to get.


To my knowledge there is no way you can achieve that.
Simply because the OS manages running threads and distributes resources according to it's scheduler.

Edit:
Since your goal is to have a "spare" core to run other processes on I'd suggest you use a thread manager and get the number of cores on the system (x) and then spawn at most x-1 threads on the specific system. That way you'll have your spare core.

The former statements still apply, you cannot specify which cores to run threads on unless you in the OS specify it. But from java, no.


Short of assigning the entire JVM to a single core, I'm not sure how you'd be able to do this. In Linux, you can use taskset:

http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html

I suppose you could run your JVM within a virtualized environment (e.g., VirtualBox/VMWare instance) with one processor allocated, but I'm not sure that that gets you what you want.


I read this as asking if a Java application can control the thread affinity itself. Java does not provide any way to control this. It is treated as the business of the host operating system.

If anything can do it, the OS can, and they typically can, though the tools you use for thread pinning will be OS specific. (But if the OS is itself is virtualized, there are two levels of pinning. I don't know if that is going to work / be practical.)


There don't appear to be any relevant Hotspot JVM thread tuning options in modern JVMs.

If you were using a Rockit JVM you could choose between "native threads" (where there is a 1-1 mapping between Java and OS threads) and "thin threads" where multiple Java threads are multiplexed onto a small number of OS threads. But AFAIK, JRocket "thin threads" are only supported in 32bit mode, and they don't allow you to tune the number of OS threads used.

This is really the kind of question that you should be asking under a Sun support contract. They have people who have spent years figuring out how to get the best performance out of big Java apps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜