1

我已经开始使用 fj JAVA 框架了。
据我了解,它只是将算法计算等大任务划分为小线程。
问题是是否值得在单处理器机器上使用这个框架。
谢谢

4

5 回答 5

2

不。

单个处理器(单个单核处理器)不能利用多个线程,因为它一次只能运行一个线程。事实上,线程管理的开销会稍微减慢您的计算速度。

相关:您可以在运行时获取应用程序可用的处理器数量。见Runtime.availableProcessors()

于 2013-10-15T15:19:14.310 回答
1

Usually you use the number of processors at runtime to determine the configuration of your execution.

Even if you have a single core machine, you may have the ability to execute more than one thread at time, therefore you can leverage this using f.e. fork-join. If you really have just a single execution thread on your cpu, you will have a small overhead from using a multithreading framework compared to plain execution.

On the other hand: If you write your application on a single core machine and have no profit from it, you may be able to use a different machine later, or give the application to users which have multicore/threading machines. Without building the application again or change anything you can make use of that capabilities then.

So it really depends on the way you expect to use your application now - and in the future.

于 2013-10-15T15:22:25.477 回答
1

正如 oracle 文档JAVA FORK/JOIN中所写:

fork/join 框架是 ExecutorService 接口的实现,可帮助您利用多个处理器。它专为可以递归分解成较小部分的工作而设计。目标是使用所有可用的处理能力来提高应用程序的性能。

所以它的主要目标是多处理器架构。

于 2013-10-16T07:34:11.263 回答
0

如果有大量 IO 操作,多线程在 1 核处理器上仍然有用。否则它甚至会受到伤害。

于 2013-10-15T15:18:43.290 回答
0

每个处理器都有一个线程调度器,它可以将时间分配给一个线程来完成它的工作。因此,如果您有 100 个线程和一个处理器,调度程序将在这 100 个线程之间切换。然而,话虽如此,在单个处理器上使用多个线程并没有任何优势,因为正如我所描述的,实际上它发生的事情是调度程序在线程之间切换得非常快。另外,从 CPU 的角度来看,上下文切换非常慢。

于 2013-10-15T15:31:12.757 回答