Java threads and number of cores

后端 未结 3 766
一个人的身影
一个人的身影 2020-11-29 17:20

I just had a quick question on how processors and threads work. According to my current understanding, a core can only perform 1 process at a time. But we are able to prod

3条回答
  •  时光取名叫无心
    2020-11-29 17:39

    In short, your understanding of a core is correct. A core can execute 1 thread (aka process) at a time.

    However, your program doesn't really run 30 threads at once. Of those 30 threads, only 4 are running at a time, and the other 26 are waiting. The CPU will schedule threads and give each thread a slice of time to run on a core. So the CPU will make all the threads take turns running.

    A common misconception:

    Having more threads will make my program run faster.

    FALSE: Having more threads will NOT always make your program run faster. It just means the CPU has to do more switching, and in fact, having too many threads will make your program run slower because of the overhead caused by switching out all the different processes.

提交回复
热议问题