简体   繁体   English

等待Executor中的所有线程完成?

[英]Wait for all threads in an Executor to finish?

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. 我正在实现一个parellel quicksort作为编程实践,在我完成后,我阅读了Executors上的Java教程页面,听起来他们可以让我的代码更快。 Unfortunately, I was relying on join()'s to make sure that the program doesn't continue until everything is sorted. 不幸的是,我依靠join()来确保程序不会继续,直到所有内容都被排序。 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);
    }
}

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. 这似乎工作正常,但如果我在调用我的非递归quicksort()方法后立即运行e.shutdown(),它有一堆RejectedExecutionExceptions,所以我认为这不像我想要的那样好。

So anyway, I'm basically trying to get the same functionality as leftThread.join() but with an Executor, and my questions is: 所以无论如何,我基本上试图获得与leftThread.join()相同的功能,但是使用Executor,我的问题是:

Is this the best way to wait until all of the threads are done? 这是等待所有线程完成的最佳方式吗?

EDIT: 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. 编辑:好的,所以我弄清楚为什么在关闭我的Executor后出现了一堆错误,这是因为我在循环中调用了这个函数(甚至运行时间)并且没有创建新的Executor。

What type of executor are you using? 你使用什么类型的执行者?

ThreadPoolExecutor .awaitTermination() will do what you are asking about (it's effectively a bulk join operation). ThreadPoolExecutor .awaitTermination()将执行您所要求的(它实际上是批量连接操作)。

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). 总而言之,ThreadPoolExecutor将允许你设置线程数等的限制......(如果线程数很高,可能比你正在做的递归更好,不确定)。

PS - I doubt that executors will make your code run any faster, but they may make your code easier to read and maintain. PS - 我怀疑执行程序会使代码运行得更快,但它们可能使代码更易于阅读和维护。 Using a Thread pool will make things faster for this sort of algorithm, and the Executor makes it easy to work with thread pools. 使用线程池可以使这种算法更快,Executor使得使用线程池变得更容易。

看一下Executors.newFixedThreadPool ,它允许你创建一个最多n个线程的池(删除你的“if”)和ExecutorService.shutdown方法以及ExecutorsService.awaitTermination方法。

您可以使用CountDownLatch

PS - I doubt that executors will make your code run any faster, but they may make your code easier to read and maintain. PS - 我怀疑执行程序会使代码运行得更快,但它们可能使代码更易于阅读和维护。 Using a Thread pool will make things faster for this sort of algorithm, and the Executor makes it easy to work with thread pools. 使用线程池可以使这种算法更快,Executor使得使用线程池变得更容易。

This is not correct. 这是不正确的。

The executor can be 'backed' by any number of different execution systems including pooled threads. 执行程序可以由任意数量的不同执行系统“支持”,包括池化线程。

You need to call the factory class correctly. 您需要正确调用工厂类。

Furthermore you also need to decide on a policy for dealing with situations where jobs are submitted to the queue faster than they can be consumed, because you may not initially run out of memory due to limits on the thread execution, but if you queue millions of jobs, then they have to be stored some place whilst they wait for execution. 此外,您还需要确定一个策略来处理将作业提交到队列的速度快于可以使用的情况,因为由于线程执行的限制,您最初可能不会耗尽内存,但如果排队数百万工作,然后他们必须在等待执行时存储在某个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM