2013-08-30

Java: Simple multithreading a piece of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
import java.util.concurrent.*;
 
// ...
 
ExecutorService exec = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors());
try {
    for (int i = 0; i < num_of_runs; i++) {
        exec.submit(new Runnable() {
            @Override
            public void run() {
                // do stuff in num_of_runs times through a 2*#CPU threadpool
            }
        });
    }
} finally {
    exec.shutdown();
}

No comments :

Post a Comment