search

Home  >  Q&A  >  body text

java - 为什么只能用单核cpu的软件并不是将一个cpu线程独占,而是每个线程平均占用?

比如说,我cpu有四个线程,那么运行一个只能利用单线程的软件为什么不是这个进程把第一个核心的所有资源占用,而是每个线程占用百分之25?

怪我咯怪我咯2769 days ago900

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-18 09:54:36

    What you want can be achieved. There are different APIs that can be called on different operating systems. On Linux, sched_setaffinity is called. On Windows, SetProcessAffinityMask can be used. This is to bind a specific thread to run on a specific CPU. run on the core. Of course, what you said is that a thread completely occupies all the running of a CPU. This cannot be realized in the user mode. The current operating system schedules the program out; the kernel mode can run, but you have to actively schedule it yourself. Otherwise , the entire machine will appear in suspended animation, and the machine will not respond, but it is actually an infinite loop.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:54:36

    Modern operating systems operate the CPU using a preemptive multi-threading mechanism. When acquiring CPU control, a time slice is obtained, and after the time slice ends, the control is handed over to other threads.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:54:36

    Home PC is a time-sharing system, which uses time slice rotation to evenly allocate resources. What should I do if a single thread monopolizes the entire CPU core and the thread loops endlessly

    reply
    0
  • Cancelreply