Sunday, 8 September 2013

MultiThread Java Only Utilize One Thread

MultiThread Java Only Utilize One Thread

I'm working on this project and I want to utilize multi-thread in my code.
So I developed this little piece of code and tested it but it turned out
that it only uses one of the threads in my computer. Can someone please
tell me what's wrong with it and how I can improve it?
public static int choiceCount(List<Character> charlist) throws
InterruptedException, ExecutionException {
int coreCount = 8;
ExecutorService e1 = Executors.newFixedThreadPool(coreCount);
Integer total = 0;
for (int i = 0; i < coreCount; i++) {
Future<Integer> result = e1.submit(new Count(coreCount, i,
charlist));
total += result.get();
}
e1.shutdown();
return total;
}
And here's the Callable
class Count implements Callable<Integer> {
//where the processing code is
}
So when I run this program, it only uses 12.5% of my CPU which is one
thread only... Ideas guys?
Thanks

No comments:

Post a Comment