我想用 Java 中的多线程解决数学问题。我的数学问题可以分为多个工作单元,我想在多个线程中解决。
我不想让固定数量的线程在其上工作,而是让线程数量与 CPU 核心数量相匹配。我的问题是,我无法在互联网上找到简单的教程。我发现的都是具有固定线程的示例。
这怎么能做到呢?你能提供例子吗?
I want to solve a mathematical problem with multiple threads in Java. my math problem can be separated into work units, that I want to have solved in several threads.
I don't want to have a fixed amount of threads working on it, but instead an amount of threads matching the amount of CPU cores. My problem is, that I couldn't find an easy tutorial in the internet for this. All I found are examples with fixed threads.
How can this be done? Can you provide examples?
发布评论
评论(6)
您可以使用静态运行时方法来确定 Java 虚拟机可用的进程数,可用处理器。一旦确定了可用处理器的数量,就可以创建该数量的线程并相应地分配工作。
更新:为了进一步澄清,线程只是 Java 中的一个对象,因此您可以像创建任何其他对象一样创建它。因此,假设您调用上述方法并发现它返回 2 个处理器。惊人的。现在,您可以创建一个循环来生成新线程,并拆分该线程的工作,然后触发该线程。下面是一些伪代码来演示我的意思:
有关创建自己的线程的更多信息,前往本教程。另外,您可能需要查看线程池来创建线程。
You can determine the number of processes available to the Java Virtual Machine by using the static Runtime method, availableProcessors. Once you have determined the number of processors available, create that number of threads and split up your work accordingly.
Update: To further clarify, a Thread is just an Object in Java, so you can create it just like you would create any other object. So, let's say that you call the above method and find that it returns 2 processors. Awesome. Now, you can create a loop that generates a new Thread, and splits the work off for that thread, and fires off the thread. Here's some pseudocode to demonstrate what I mean:
For more information on creating your own thread, head to this tutorial. Also, you may want to look at Thread Pooling for the creation of the threads.
您可能还想查看 java.util.concurrent 框架来了解这些内容。
像这样:
或者
这比处理你自己的线程池等要好得多。
You probably want to look at the java.util.concurrent framework for this stuff too.
Something like:
or
This is a lot nicer than coping with your own thread pools etc.
选项 1:
newWorkStealingPool
使用此 API,您无需将核心数传递给
ExecutorService
。此 API 的实现来自 grepcode
选项 2:
newFixedThreadPool 来自
Executors
或其他 newXXX 构造函数
,返回ExecutorService
将 nThreads 替换为
Runtime.getRuntime().availableProcessors()
选项 3: >
ThreadPoolExecutor
通过
Runtime.getRuntime().availableProcessors()
作为maximumPoolSize
的参数。Option 1:
newWorkStealingPool from
Executors
With this API, you don't need to pass number of cores to
ExecutorService
.Implementation of this API from grepcode
Option 2:
newFixedThreadPool API from
Executors
orother newXXX constructors
, which returnsExecutorService
replace nThreads with
Runtime.getRuntime().availableProcessors()
Option 3:
ThreadPoolExecutor
pass
Runtime.getRuntime().availableProcessors()
as parameter tomaximumPoolSize
.Doug Lea(并发包的作者)有一篇可能相关的论文:
http://gee.cs.oswego.edu/dl/papers/fj.pdf
Fork Join 框架已添加到 Java SE 7 中。以下是更多参考资料:
http://www.oracle.com/technetwork /articles/java/fork-join-422606.html
Doug Lea (author of the concurrent package) has this paper which may be relevant:
http://gee.cs.oswego.edu/dl/papers/fj.pdf
The Fork Join framework has been added to Java SE 7. Below are few more references:
http://www.ibm.com/developerworks/java/library/j-jtp11137/index.html
Article by Brian Goetz
http://www.oracle.com/technetwork/articles/java/fork-join-422606.html
标准方法是 Runtime.getRuntime().availableProcessors() 方法。
在大多数标准 CPU 上,您将在此处返回最佳线程数(不是实际的 CPU 核心数)。因此,这就是您正在寻找的。
示例:
不要忘记像这样关闭执行程序服务(否则您的程序将不会退出):
这里只是一个快速概述如何设置基于未来的 MT 代码(题外话,用于说明):
然后您需要跟踪您期望有多少结果并像这样检索它们:
The standard way is the Runtime.getRuntime().availableProcessors() method.
On most standard CPUs you will have returned the optimal thread count (which is not the actual CPU core count) here. Therefore this is what you are looking for.
Example:
Do NOT forget to shut down the executor service like this (or your program won't exit):
Here just a quick outline how to set up a future based MT code (offtopic, for illustration):
Then you need to keep track on how many results you expect and retrieve them like this:
在 Runtime 类上,有一个名为 availableProcessors() 的方法。您可以使用它来计算您有多少个 CPU。由于您的程序受 CPU 限制,因此您可能希望每个可用 CPU (最多)有一个线程。
On the Runtime class, there is a method called availableProcessors(). You can use that to figure out how many CPUs you have. Since your program is CPU bound, you would probably want to have (at most) one thread per available CPU.