Java:どのように規模のスレッドによるcpuコア?
-
22-09-2019 - |
質問
私は良いものではないjavaプログラマで趣味がいものからの平均よりもかんでくる。
思いを解決する数理的問題を複数のスレッドjava.私の数学の問題を分離する作業台は、たいている複数のスレッド)。
ものにならないようにしたい一定量のスレッドして取り組むというものではなく、coresponding量のスレッドのcpuコアを用いた私の問題であることが見出せなかったので簡単チュートリアルインターネット。すべての発見例は固定のスレッド)。
できないので、代わりに助けてくれていtuturialただくと良いですか。そうするん:)
解決
を判定する工程数のご用意のJava仮想マシン用の静的ランタイム方法 availableProcessors.定が決まった数のプロセッサー、作成するスレッド数は分割のお仕事です。
更新:付けをより明確にするため、スレッドがオブジェクトにJavaとして購入することができますこのように作成その他のオブジェクトです。いうでしょうかを呼び出す方法を探ることは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
このは、など、独自のスレッドプールに対処するよりも多くの進歩しています。
ダグ・リー(同時パッケージの作者)が関連する可能性がある。この論文があります。 http://gee.cs.oswego.edu/dl/papers/fj.pdf の
フォークは、フレームワークに参加は、Java SE 7の下に追加されているいくつかのより多くの参照です。
http://www.ibm.com/developerworks/ Javaの/ライブラリ/ J-jtp11137 / index.htmlをする ブライアン・ゲッツの記事
http://www.oracle.com/technetwork/記事/ Javaの/フォーク参加-422606.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();
ここで(説明のために、offtopic)将来のベースMTコードを設定する方法だけで素早く概要:
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();
}
ランタイムクラスの、availableProcessors(と呼ばれる方法)があります。あなたが持っているどのように多くのCPUを把握するためにそれを使用することができます。あなたのプログラムがCPUバインドされているので、おそらく(最大で)利用できるCPUごとに1つのスレッドを持ちたいです。