多线程应用程序如何在多处理器系统中工作
How does multiple threaded application works in multi processor system
假设我们有一个游戏应用程序,其中汽车、自行车和塔钟 运行 在各个线程中并行运行。
据我们所知,一次只有一个线程 运行。 (线程之间的上下文切换非常快,这使得它看起来像 运行 与人眼平行)
假设游戏 运行 在双核手机 phone 中运行。
移动处理器的两个内核是否共享 3 个线程?
汽车线程和自行车线程是否有可能在任何时间点同时运行?
is there any possibility for the car thread and bike thread to run simultaneously at any point of time?
是的,有可能。这取决于 JVM 实现(因为你提到了 Java)和 OS 实现,所以不能保证,但有可能。
As we know only one thread runs at a time. (context switch happens between threads very fast which makes it look like it is running parallel to human eyes)
错了。这在单核单处理器上是正确的,在某些 JVM/OS 上可能是正确的,但在一般情况下是错误的。
Threads may be supported by having many hardware processors, by time-slicing a single hardware processor, or by time-slicing many hardware processors.
Is there any possibility for the car thread and bike thread to run simultaneously at any point of time?
即使在 single-core 机器上,线程也有可能 运行 concurrently。 运行并发是使用线程的重点。
如果您知道您的程序只会 运行 在 single-processor 平台上,那么您也许可以不尊重 Java 的 memory model,但是线程安全的基本规则——什么需要锁定,什么不需要——在任何一种情况下都是一样的。
...and a tower clock
确实没有理由使用专用线程来为时钟设置动画。时钟是绝对可预测的。它的外观是当前时间的函数,仅此而已。
P.S.: 在许多应用程序中,没有理由为个别汽车、自行车或其他 MOB 使用专用线程;但我不知道你的申请,所以我不能对此发表评论。
假设我们有一个游戏应用程序,其中汽车、自行车和塔钟 运行 在各个线程中并行运行。
据我们所知,一次只有一个线程 运行。 (线程之间的上下文切换非常快,这使得它看起来像 运行 与人眼平行)
假设游戏 运行 在双核手机 phone 中运行。
移动处理器的两个内核是否共享 3 个线程?
汽车线程和自行车线程是否有可能在任何时间点同时运行?
is there any possibility for the car thread and bike thread to run simultaneously at any point of time?
是的,有可能。这取决于 JVM 实现(因为你提到了 Java)和 OS 实现,所以不能保证,但有可能。
As we know only one thread runs at a time. (context switch happens between threads very fast which makes it look like it is running parallel to human eyes)
错了。这在单核单处理器上是正确的,在某些 JVM/OS 上可能是正确的,但在一般情况下是错误的。
Threads may be supported by having many hardware processors, by time-slicing a single hardware processor, or by time-slicing many hardware processors.
Is there any possibility for the car thread and bike thread to run simultaneously at any point of time?
即使在 single-core 机器上,线程也有可能 运行 concurrently。 运行并发是使用线程的重点。
如果您知道您的程序只会 运行 在 single-processor 平台上,那么您也许可以不尊重 Java 的 memory model,但是线程安全的基本规则——什么需要锁定,什么不需要——在任何一种情况下都是一样的。
...and a tower clock
确实没有理由使用专用线程来为时钟设置动画。时钟是绝对可预测的。它的外观是当前时间的函数,仅此而已。
P.S.: 在许多应用程序中,没有理由为个别汽车、自行车或其他 MOB 使用专用线程;但我不知道你的申请,所以我不能对此发表评论。