Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, How can I create threads so that each thread is dedicatedly running in one core? [duplicate]

Possible Duplicate:
Java thread affinity

I have a server and it has a 16 cores cpu.

In Java, I need to create some threads (the number of threads is less than 16). Each thread needs to run some operations, e.g., process an event queue.

How can I create these threads so that it guarantees that each thread is assigned to one core forever? I mean I don't want the OS exchanging cores for one thread. I just wish one thread is dedicatedly running on a fixed core.

Can I do that?


The reason I want this is

I handle some background tasks (computation intensive) and some user-facing tasks in the same server. I don't want the user side get any negative impact. For example, if my computation tasks are assigned to 16 cores, surely the threads which are running for user side will be negatively affected, right?

like image 729
Jackson Tale Avatar asked Jan 17 '23 18:01

Jackson Tale


2 Answers

You cant. JVM virtualizes all hardware so you can not do anything like that.

There could be some 'tricks' that would work on some specific architecture and some specific JVM, but it would all be hackish and unreliable.

like image 163
bezmax Avatar answered Jan 20 '23 14:01

bezmax


Don't waste your valuable development time on this. Fix some other problems. If the time taken by OS core menagement is an issue for your app, it's teetering on the edge of failure anyway.

like image 40
Martin James Avatar answered Jan 20 '23 15:01

Martin James