How to use exactly 8 threads for expensive parts all the time?
I have a problem with a number crunch, for which I created a simple structure. My problem is to find an elegant and easy way to optimally use all the processor cores.
To get good performance, I use a fixed-size stream pool of 8. The idea is to use as many threads as there are hardware threads for optimal performance.
The simplified use of pseudocode in the structure is as follows:
interface Task {
data[] compute(data[]);
}
Task task = new Loop(new Chain(new DoX(), new DoY(), new Split(2, new DoZ())));
result = task.compute(data);
- The Loop Task will run until some completion criteria are met.
- The chain defines the chain of tasks (for example, in the above r = t1.compute (r); r = t2.compute (r); r = t3.compute (r); return r;)
- Split Task will split the data and complete the task in parts (for example, create 2 parts and return new data [] {t1.compute (part1), t1.compute (part2)})
Currently, stream processing is implemented in the separation task. Thus, the Split task will transfer the calculations of t1.compute (part1) and t1.compute (part2) to the thread pool.
Approach 1, possibly a complete dead end
, Split Task get() . , Split Task Split Task, wait future.get() , Split Task . , 8 . , , , .
1) , future.get() , ? , , .get(), , ? [ , ]
2, , , -
( ) , (partN) . , , partN-1, , . ( partN-1, partN-2... .
1), , , future.get() , .
3, ,
, 2 , . , (?) .
3.a.: , Split , , , . , . , Split Task, , , .
3.b.: , Split , , . Split Task, , .
2a) ?
2b) , , ? ?
.
3) 8 ? , ? , JVM ?
4) ?
[EDIT]
- ( 2)
ForkJoinPool .
. , ForkJoinPool (. ). .
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinPool.html
" , IO ".
" 32767"
http://homes.cs.washington.edu/~djg/teachingMaterials/grossmanSPAC_forkJoinFramework.html
" ForkJoin , - 100 10000."
" " , 10 000 . / , , .
, 3a - , , ForkJoin, , , , , , , ( , , OS, , , ).
, 2 : , , , ForkJoinPool, , , .
, Split ( Split node, ), future.get(), , future.isDone(). , , . , , , .
: , , , ( , , , ?)
, , ThreadPool Split Task, - .
: ThreadPool - ThreadPool (, Job). ThreadPool .
, , . , . .
:
, "" , , . , . , , , ( , 2 ). ForkJoinPool, managedBlock() ForkJoinPool.ManagedBlocker, . , CompletionService , 2- , , , , , ForkJoinPool.