KHALID
KHALID

Reputation: 61

Run Java Threads On One CPU

We Have a Multi-threaded Application in JAVA which has multiple threads running in parallel. Now we want to run all these threads on a single core. Currently application is running on a system having more then one Cores.

We know there is a technique available ProcesAffinity in .Net Framework to set process affinity.

But we don't want to depend on .Net Framework, because our application is build in java.

Do we set Process affinity using Bat file and run our application executable jar file through Bat file?

Currently our application is running on Window XP. So we need a solution that should be working fine on XP platform.

Upvotes: 1

Views: 2923

Answers (3)

beny23
beny23

Reputation: 35068

Personally, I don't think that the fact that this cannot be set in pure Java is a bad thing, as to me, how an app is run does very much depend on the OS, so therefore a OS-specific solution isn't a bad thing.

You can use the MS psexec utility to set the affinity:

psexec -a 1 java -jar myapplication.jar

Would instruct that all of the threads created by java would be run on the lowest CPU.

And this line would be your .BAT file...

Upvotes: 1

Azodious
Azodious

Reputation: 13882

EDIT:

It's possible: See Java thread affinity


Pure Java doesn't support running a thread on specific processor. Check the SO question linked above.

Upvotes: 4

Stephen C
Stephen C

Reputation: 719739

You cannot do it in pure Java. But on some versions of Windows, you can do it via operating system utilities; see https://superuser.com/questions/309617/how-to-limit-a-process-to-a-single-cpu-core ... and you might be able to do this by calling native libraries via JNI.

Upvotes: 0

Related Questions