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

后端 未结 2 1091
清酒与你
清酒与你 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: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

提交回复
热议问题