views:

304

answers:

4

I would like a solution that doesn't include critical sections or similar synchronization alternatives. I'm looking for something similar the equivalent of Fiber (user level threads) from Windows.

+2  A: 

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.

Sani Huttunen
+12  A: 

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.

Jeremy Petzold
I'm not using Windows and I'm testing my application on a cluster. I'm looking for something at runtime.
Alexandru
Run it off the cluster then.
Jeremy Petzold
A: 

Java does not provide any way to control the allocation of threads to physical processors. That is (rightly) the business of the host operating system. If anything can do it, the OS can.

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

If you are using using the Oracle Rockit JVM you can 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.

Stephen C
A: 

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.

Jack Leow