开发者

How does Java handle multithreading?

开发者 https://www.devze.com 2022-12-24 08:41 出处:网络
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 cor开发者_开发技巧e?

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 cor开发者_开发技巧e?

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


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消