Wait until all threads in the Contractor run out?

I use quicksort parellel as a programming practice, and after I finished, I read the Java tutorial page on Executors, which sounds like they can make my code even faster. Unfortunately, I relied on join () to make sure that the program would not continue until everything was sorted. Now I am using:

public static void quicksort(double[] a, int left, int right) {
    if (right <= left) return;
    int i = partition(a, left, right);

    // threads is an AtomicInteger I'm using to make sure I don't
    // spawn a billion threads.
    if(threads.get() < 5){

        // ThreadSort run method just calls quicksort()
        Future leftThread = e.submit(new ThreadSort(a, left, i-1));
        Future rightThread = e.submit(new ThreadSort(a, i+1, right));

        threads.getAndAdd(2);
        try {
            leftThread.get();
            rightThread.get();
        }
        catch (InterruptedException ex) {}
        catch (ExecutionException ex) {}
    }
    else{
        quicksort(a, left, i-1);
        quicksort(a, i+1, right);
    }
}

, , e.shutdown() quicksort(), RejectedExecutionExceptions, , , .

, , leftThread.join(), , :


, ?


EDIT: , , Executor, ( ), Executor.

+5
4

?

ThreadPoolExecutor .awaitTermination() , ( ).

, ThreadPoolExecutor .. ( , , , , , ).

PS - , , . , Executor .

+9
+2

PS - , , . , .

.

"" , .

factory.

, , , , - - , , , .

+1
source

All Articles