等待执行程序中的所有线程完成? [英] Wait for all threads in an Executor to finish?

查看:123
本文介绍了等待执行程序中的所有线程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施并行快速排序作为编程实践,完成后,我阅读了Executor上的Java教程页面,听起来他们可以使我的代码更快.不幸的是,我依靠join()来确保程序在所有内容都整理好之后才继续运行.现在我正在使用:

I'm implementing a parellel quicksort as programming practice, and after I finished, I read the Java tutorial page on Executors, which sound like they could make my code even faster. Unfortunately, I was relying on join()'s to make sure that the program doesn't continue until everything is sorted. Right now I'm 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's 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);
    }
}

这似乎可以正常工作,但是如果我在调用非递归quicksort()方法后立即运行e.shutdown(),则它有很多RejectedExecutionExceptions,因此我认为它不如我所愿想要的.

因此,无论如何,我基本上是想获得与leftThread.join()相同的功能,但要有一个执行程序,而我的问题是:

这是等待所有线程完成的最好方法吗?

好的,所以我弄清楚了为什么关闭我的Executor之后会出现很多错误,这是因为我在循环中调用此函数(以平衡运行时间)而不创建新的Executor.

This seems to work ok, but if I run e.shutdown() right after I call my non-recursive quicksort() method, it has a bunch of RejectedExecutionExceptions, so I assume this isn't working as well as I had wanted.

So anyway, I'm basically trying to get the same functionality as leftThread.join() but with an Executor, and my questions is:

Is this the best way to wait until all of the threads are done?

Ok, so I figured out why I got a bunch of errors after shutting down my Executor, it was because I was calling this function in a loop (to even out run times) and not creating a new Executor.

推荐答案

您使用的是哪种类型的执行器?

What type of executor are you using?

ThreadPoolExecutor .awaitTermination()可以满足您的要求(实际上是批量连接操作).

ThreadPoolExecutor.awaitTermination() will do what you are asking about (it's effectively a bulk join operation).

总而言之,ThreadPoolExecutor将允许您对线程数等进行设置限制……(最好比像递归那样进行递归,如果线程数增加,不确定).

As a total aside, ThreadPoolExecutor will allow you to set limits on the # of threads, etc... (might be better than going recursive like what you are doing if the thread count goes high, not sure).

PS-我怀疑执行程序是否会使您的代码运行得更快,但它们可能会使您的代码更易于阅读和维护.使用线程池将使使用这种算法的速度更快,并且Executor使使用线程池的工作变得容易.

PS - I doubt that executors will make your code run any faster, but they may make your code easier to read and maintain. Using a Thread pool will make things faster for this sort of algorithm, and the Executor makes it easy to work with thread pools.

这篇关于等待执行程序中的所有线程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆