views:

171

answers:

4

How does Java decide which core to assign a thread or a process? Is there any way to control that? to prevent two large threads from executing on the same core?

Basically what I am asking is for further information on either how multi-threading works in Java, or how to control it within Java.

+2  A: 

Here's a tutorial on Multithreading in Java.

As for the thread scheduling - the operating system should handle scheduling the threads appropriately. You typically do not need to worry about this.

The Java Thread class does not currently provide a method to set the thread affinity manually, although this has been proposed in the past.

Reed Copsey
+2  A: 

You can't set processor affinity for specific Threads. But if you split your program into two processes, you should be able to assign those processes to specific processors at the OS level.

http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html

Tim Bender
@Tim Bender: +1... and cue the (clueless) comments explaining that setting processor affinity serves no purpose in 3..2..1 :)
Webinator
A: 

This really depends on the implementation on the JVM, but in general, Java implementations rely on the underlying OS's threading functionality. To the best of my knowledge there are no public and standard extensions to set an affinity. There may be experimental JVMs that offer hooks, however.

Furthermore, interfering with the JVM abstraction to mess directly with the underlying platform goes, to a degree (and IMHO), against the spirit of Java.

Uri
+1  A: 

How does Java decide which core to assign a thread or a process?

It doesn't. The operating system does.

Is there any way to control that? to prevent two large threads from executing on the same core?

Not within Java.

Basically what I am asking is for further information on either how multi-threading works in Java, or how to control it within Java.

There isn't any. It is all done by the OS.

Basically you are asking the wrong question.

EJP
The question sort of makes a little more sense for green threads.
Thorbjørn Ravn Andersen
But are there any green threads left in the world?
EJP