Why does a single threaded process execute on several processors/cores?

后端 未结 2 1084
清酒与你
清酒与你 2020-12-13 22:32

Say I run a simple single-threaded process like the one below:

public class SirCountALot {
    public static void main(String[] args) {
        int count = 0         


        
相关标签:
2条回答
  • 2020-12-13 23:09

    I would also expect this could well be done on purpose by the CPU and OS so as to try and spread the thermal load on the CPU die...

    So it would rotate the (unique/single) thread from core to core.

    And that could admittedly be an argument against trying to fight this too hard (especially as, in practice, you often will see better improvements by simply tuning / improving the app itself anyway)

    0 讨论(0)
  • 2020-12-13 23:10

    The OS is responsible for scheduling. It is free to stop a thread and start it again on another CPU. It will do this even if there is nothing else the machine is doing.

    The process is moved around the CPUs because the OS doesn't assume there is any reason to continue running the thread on the same CPU each time.

    For this reason I have written a library for lock threads to a CPU so it won't move around and won't be interrupted by other threads. This reduces latency and improve throughput but does tire up a CPU for that thread. This works for Linux, perhaps you can adapt it for Windows. https://github.com/peter-lawrey/Java-Thread-Affinity/wiki/Getting-started

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