Question

What is the way of telling a thread to change the core that it is working on programmatically?

Lets say i am using FX-8150 which has some cores but i want one of my threads to work in especially the 5.core. Is this possible? If yes, can you give a sample of Java please? The thread may be another process' thread or the current process' thread.

My pseudo example:

time interval 1 : my_thread working in first core at 5GHz while other cores are 3.6 GHz
time interval 2 : my_thread working in second core at 5GHz while other cores are 3.6 GHz
time interval 3 : my_thread working in third core at 5GHz while other cores are 3.6 GHz
time interval 4 : my_thread working in fourth core at 5GHz while other cores are 3.6 GHz
time interval 5 : my_thread working in fifth core at 5GHz while other cores are 3.6 GHz
time interval 6 : my_thread working in sixth core at 5GHz while other cores are 3.6 GHz
time interval 7 : my_thread working in seventh core at 5GHz while other cores are 3.6 GHz
time interval 8 : my_thread working in eighth core at 5GHz while other cores are 3.6 GHz

All other cores will be needed to be idle.

Était-ce utile?

La solution

I have a library called Java Thread Affinity which allows you to assign running threads to logical thread or cores.

In your case, you need to allow one core for the OS as a minimum, and you can reserve all the remaining cores in Linux. The thread library assigns threads to these reserved cores. The reason you want reserved cpus is to prevent other threads running on that cpu. You can tell the library to assign a thread to a logical cpu, or a whole core. You can assign threads to be on the same or different core or socket as another thread.

https://github.com/peter-lawrey/Java-Thread-Affinity/wiki/Getting-started

The library currently supports Windows and Linux.

Autres conseils

Assuming you are using Windows 7. Find the process in Task Manager, right click on it, select set affinity, choose which core you want it to use.

http://www.sevenforums.com/tutorials/83632-processor-affinity-set-applications.html

Programatically:

You can permanently set a program's affinity by creating a .bat batch file in the same folder as the program, with the contents:

start /affinity 1 program_name.exe

start /affinity 1 <invoke your_java_program>

This will force the program to use only cpu core 0. use ... /affinity 2 ... to specify cpu core 1 etc

You should then modify the program's start-menu and/or desktop shortcut to point to your newly created batchfile.

a tiny drawback is that you will see a small cmd window briefly flash on the screen before your program opens.

for extra performance, you can add /high to give the application a higher cpu priority.

start /affinity 1 /high program_name.exe
start /affinity 1 <invoke your_java_program>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top