I have to write a code for concurrent Mergesort and my problem is, if the merge sort implementation wants to create a new thread, it has to check if there is a thread slot available or not ( so i created a Threapool(max 5 Threads).It is also working, but my problem is that i should only use 5 threads without reusing them.
That means if there are no more thread slots available the thread just performs for its array part the traditional recursive merge sort algorithm.
int nThreads = 5;
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
for (int i = 1; i < 10; i++){
Runnable worker = new Mergesort(array);
executor.execute(worker);
}
executor.shutdown();
while(!executor.isTerminated()) {
}