I'm using a quad core PC (Intel CORE i7) but I execute the task on 4 threads it takes 14s instead of somthing aroud 6s which is the time taken by one thread to perform the task. Is it because of all the initialisatioin (creation of the 3 other threads, ...) ?
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import junit.framework.TestResult;
import junit.framework.TestSuite;
import com.google.common.base.Stopwatch;
public class MultithreadWithExecutor {
private static class ExecuteLongTask implements Callable<Integer>{
private static final int ARRAY_SIZE = 10000;
private static final int NB_TEST_ALL = 10000;
private static final int NB_TEST_QUART = NB_TEST_ALL/4;
@Override
public Integer call() throws Exception {
for (int i = 0; i < NB_TEST_QUART; i++) {
//Create a list
List<Double> lst = new ArrayList<Double>();
for (int j = 0; j < ARRAY_SIZE; j++) {
lst.add(Math.random());
}
//sort it
Collections.sort(lst);
}
return 0;
}
}
public static void main(String[] ar) throws InterruptedException, ExecutionException {
int n = 4;
// Build a fixed number of thread pool
ExecutorService pool = Executors.newFixedThreadPool(n);
Stopwatch watch = Stopwatch.createStarted();
Future<Integer> future1 = pool.submit(new ExecuteLongTask());
Future<Integer> future2 = pool.submit(new ExecuteLongTask());
Future<Integer> future3 = pool.submit(new ExecuteLongTask());
Future<Integer> future4 = pool.submit(new ExecuteLongTask());
// Wait until threads finish
int testRuns= future1.get();
testRuns+=future2.get();
testRuns+=future3.get();
testRuns+=future4.get();
long elapsed = watch.elapsed(TimeUnit.MILLISECONDS);
// took ~14s instead of ~6s (time taken by on thread to execute the task)
System.out.println("Runned: "+testRuns+" in: "+elapsed);
pool.shutdown();
}
}