How JVM thread scheduler control threads for multiprocessors?

前端 未结 3 698
感动是毒
感动是毒 2020-12-31 12:38

I\'ve been reading Head First for multithreading. What I know about multithreading is:

When we call start() with an object of Thread

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 13:02

    The term “JVM thread scheduler” makes only sense, if we consider operating system, JVM and class library as an execution environment as a whole. Then, it’s guaranteed that this environment has a scheduler, regardless of how it is implemented.

    In most of today’s implementations the JVM will create an operating system level thread for each Java thread and does no active scheduling activity itself. But a particular JVM implementation may contain a scheduler for operating systems that don’t have one.

    E.g., for Sun’s JVM that was the case back in the last millennium. At this time, there was the option to use green threads, as opposed to native threads. Note that these threads implemented without the aid of the operating system aren’t capable of using multiple CPUs/cores.

    So in practice, when you run your example program, the operating system’s scheduler may indeed assign the second thread to a different core. However, since this is a tiny program, it’s also possible that the first thread terminates before the second even starts its actual work, and in that case, it will likely run on the same core as the first, but there is no guaranty about any particular scheduling behavior at all.

    While there is no guaranty regarding a particular scheduling behavior, most SMP libraries and tools are built on the (founded) assumption, that if there are enough runnable threads with sufficient workload, the underlying system will assign these threads to available CPU cores.

提交回复
热议问题