Thread scheduler in java with single CPU?
I have been reading that Thread scheduler in java runs only one thread at a time in a single process.
Lets say that we have one JVM running one single CPU machine. So if I start 10 threads they will be managed by the same Thread scheduler.
If one thread is run at the time, how is that concurrent? Isn't this just an illusion of concurrency?
Can I run 10 threads at the same in a single process under the same JVM?
1- If one thread run at the time, how is that concurrent? Isn't this just an illusion of concurrency?
It is both an illusion and real. Both tasks alternate with each other without your intervention. Thus, it is indeed concurrent and very useful for code organization. It would be an absolute nightmare to run multiple tasks at the same time without threading to perform the switching for you.
However, a single processor is never running multiple threads at any given moment because it can only perform a single operation at a time, and in that sense it is an illusion.
2- Can I run 10 threads at the same in a single process under the same JVM?
Yep. See https://stackoverflow.com/a/7727922/998251 .
Summary: You have several thousand threads available to you.
There are different things at work here. So a single cpu core can only run a single kernel thread at a time. How many kernel threads may be running for a single JVM process is dependent on the JVM. Even with a single kernel thread, though, having multiple Java threads can increase your concurrency. While each thread will have to timeslice it allows a non blocked thread to perform operations while another is blocked leading to an overall lower execution time. Imagine thread A makes a network call and is waiting on the result. Thread B could take over while A is waiting and perform some calculations. If it was only synchronous the calculations B performed couldn't start until the network call returned.
链接地址: http://www.djcxy.com/p/79348.html上一篇: 深拷贝vs浅拷贝
下一篇: 线程调度程序在Java与单个CPU?