multithreading code
limited the number of threads by number of phases