Java:如何扩展线根据cpu核心?
-
22-09-2019 - |
题
我不是一个好java程序,这只是我的业余爱好,但是我想知道超过平均值的东西。
我要解决一个数学问题与多个线程。我的数学问题可以分为工作单位,我想要有解决的几个线程。
但我不想要有一个固定金额的线上工作,而是一个着相应数量的线量的cpu核心。和我的问题是,我找不到一个简单的教程在互联网上。我所找到的实例是与固定的螺纹。
所以你能不能帮我一个链接到一个很好的tuturial或可以给我一个简单的和良好的例子吗?这将是真正的好:)
解决方案
你可以确定数量的进程提供Java虚拟机,通过使用静态运行的方法, availableProcessors.一旦确定可用处理器的数量,创造数线,并分割了你的工作。
更新:为了进一步阐明,一个线程只是一个象,所以你可以创建这样你会创造任何其他目的。所以,我们说,你的呼叫上述方法,并找到其返回的2处理器。真棒。现在,您可以创建一个循环中,产生一个新的线,并拆分的工作关的该线和火灾断线。这里的一些psuedocode来证明我的意思是:
int processors = Runtime.getRuntime().availableProcessors();
for(int i=0; i < processors; i++) {
Thread yourThread = new AThreadYouCreated();
// You may need to pass in parameters depending on what work you are doing and how you setup your thread.
yourThread.start();
}
其他提示
您可能想看看这个东西太java.util.concurrent的框架。 是这样的:
ExecutorService e = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
// Do work using something like either
e.execute(new Runnable() {
public void run() {
// do one task
}
});
或
Future<String> future = pool.submit(new Callable<String>() {
public String call() throws Exception {
return null;
}
});
future.get(); // Will block till result available
这是一个要好很多,比自己的线程池等应对。
Doug Lea的(并发包的作者)具有本文这可能是相关的: http://gee.cs.oswego.edu/dl/papers/fj.pdf
在叉加入框架已添加到Java SE 7,下面是一些更多的参考资料:
http://www.ibm.com/developerworks/的Java /库/ J-jtp11137 / index.html的 文章通过布赖恩戈茨
备选案文1:
newWorkStealingPool 从 Executors
public static ExecutorService newWorkStealingPool()
创建了一个工作窃取线的游泳池采用所有可用的处理作为其目标并行水平。
与此API,你不需要通行证数量的核心, ExecutorService
.
执行这API grepcode
/**
* Creates a work-stealing thread pool using all
* {@link Runtime#availableProcessors available processors}
* as its target parallelism level.
* @return the newly created thread pool
* @see #newWorkStealingPool(int)
* @since 1.8
*/
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
备选2:
newFixedThreadPool API Executors
或 other newXXX constructors
, 返回 ExecutorService
public static ExecutorService newFixedThreadPool(int nThreads)
替换nThreads与 Runtime.getRuntime().availableProcessors()
备选案文3:
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
通 Runtime.getRuntime().availableProcessors()
作为参数 maximumPoolSize
.
的标准方法是在调用Runtime.getRuntime()。availableProcessors()方法。 在大多数标准CPU你会回到最佳的线程数(这是不实际的CPU核心数)在这里。因此,这是你在找什么。
示例:
ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
不要忘记关闭这样的执行服务(或你的程序将不会退出):
service.shutdown();
下面只是一个快速轮廓如何设置一个未来基于MT代码(offtopic,为了说明):
CompletionService<YourCallableImplementor> completionService =
new ExecutorCompletionService<YourCallableImplementor>(service);
ArrayList<Future<YourCallableImplementor>> futures = new ArrayList<Future<YourCallableImplementor>>();
for (String computeMe : elementsToCompute) {
futures.add(completionService.submit(new YourCallableImplementor(computeMe)));
}
然后,你需要保持对你期望多少结果跟踪和检索它们是这样的:
try {
int received = 0;
while (received < elementsToCompute.size()) {
Future<YourCallableImplementor> resultFuture = completionService.take();
YourCallableImplementor result = resultFuture.get();
received++;
}
} finally {
service.shutdown();
}
在Runtime类,有一种方法称为availableProcessors()。你可以用它来找出你有多个CPU。因为你的程序是CPU绑定,你可能会想有(最多)每个可用的CPU一个线程。