How does Java handle multithreading?

后端 未结 4 581
误落风尘
误落风尘 2020-12-19 17:01

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?

Basi

相关标签:
4条回答
  • 2020-12-19 17:42

    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

    0 讨论(0)
  • 2020-12-19 17:44

    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 讨论(0)
  • 2020-12-19 17:46

    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.

    0 讨论(0)
  • 2020-12-19 17:50

    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.

    0 讨论(0)
提交回复
热议问题