Search code examples
javaparallel-processing

Can we speed-up CPU intensive tasks in java?


Task like finding factorial of 2000 where using BigInteger is a CPU-intensive task, is there anyway to speed-up such processes?

Ex: finding 2000! Since it is a single task only, i think there is no need of thread here( as running this program or running this task in a thread both has to perform such CPU-intensive things).

I've heard that Java 7 introduced a new parallel mechanism for compute intensive tasks. So, how do i perform this kind of things in it?


Solution

  • A factorial could be easily split to two tasks with a final merge. This is some kind of a map-reduce, if you like.

    Example:

    8! = (7*5*3*1) * (8*6*4*2)
    

    So you can have two tasks.

    This can be generalized into any amount of parallel tasks.

    This solution has nothing to do with Java in specific, it's about converting "regular" solutions to parallel ones.