Parallelized algorithm using threads in Java