4

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?

Community
  • 1
  • 1
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271

4 Answers4

5

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.

bezmax
  • 25,562
  • 10
  • 53
  • 84
2

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.

Martin James
  • 24,453
  • 3
  • 36
  • 60
1

no you cannot do that since the OS Scheduler is meant to assign threads to cores. The JVM in which your java app runs does not have access to it.

1

You shouldn't.

If you really want to, you can use native calls : Java thread affinity

But really, be sure to have a good reason to do this. Why do you think that's a good idea ?

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • i wish I could do that because 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? – Jackson Tale May 04 '12 at 11:43
  • That's a bad reason. You may manage your computer to cap the processor use, this has nothing to see with a task not leaving its core. – Denys Séguret May 04 '12 at 12:15
  • @JacksonTale Basically, just give the GUI thread the highest priority and you will be good. `Thread t = new Thread(...); t.setPriority(Thread.MAX_PRIORITY); t.start();` – bezmax May 07 '12 at 06:39