Java 是否存在具有多个队列的现成线程池(确保每个队列的串行处理)?

Java 是否存在具有多个队列的现成线程池(确保每个队列的串行处理)?,java,multithreading,concurrency,threadpool,Java,Multithreading,Concurrency,Threadpool,在我所有的任务中,我有一些必须串行处理(它们永远不能并发运行,必须按顺序处理) 我实现了为必须串行执行的每组任务创建一个单独的线程池,其中包含一个线程。这是可行的,但我没有足够的资源。我无法控制组的数量,因此可能会导致大量线程同时运行 我有没有办法用一个线程池来实现这一点?是否有一个线程池,其中包含多个阻塞队列,我可以确保每个队列的串行执行 编辑: 只是强调我在第二段中所说的:我已经解决了这个问题,为每组必须串行执行的任务提供了一个单线程线程池。不过,我不能继续使用这个解决方案。有太多的组,我不

在我所有的任务中,我有一些必须串行处理(它们永远不能并发运行,必须按顺序处理)

我实现了为必须串行执行的每组任务创建一个单独的线程池,其中包含一个线程。这是可行的,但我没有足够的资源。我无法控制组的数量,因此可能会导致大量线程同时运行

我有没有办法用一个线程池来实现这一点?是否有一个线程池,其中包含多个阻塞队列,我可以确保每个队列的串行执行

编辑: 只是强调我在第二段中所说的:我已经解决了这个问题,为每组必须串行执行的任务提供了一个单线程线程池。不过,我不能继续使用这个解决方案。有太多的组,我不能有所有这些线程

我发现了这个相关的问题,但因为它不是最近的,所以我还是创建了我的。我所做的只是试图避免重新发明轮子,但似乎我别无选择


查看Java的内置线程执行器服务

有一个单线程执行器将同步处理每个任务

针对评论部分:

在说这行不通之前,请先阅读API。

公共静态执行器服务newSingleThreadExecutor() 创建一个执行器,该执行器使用在无界队列上运行的单个工作线程。(但是请注意,如果此单个线程在关机之前的执行过程中由于故障而终止,则在需要执行后续任务时,将替换一个新线程。)任务保证按顺序执行,并且在任何给定时间都不会有多个任务处于活动状态。与其他等效的newFixedThreadPool(1)不同,返回的执行器保证不可重新配置以使用其他线程

注意:is状态保证按顺序执行

编辑:

既然我更好地理解了你的问题,我想你可以试试。如果为每个组维护一个队列,则可以从每个队列中提取项目,并将其馈送到线程池中。下面的代码不会对任何一个组进行优先级排序,它只是以一种循环掠夺的方式吸引他们。如果您需要添加优先级,您应该能够轻松地添加。下面的代码将使用两个线程(加上管理队列的线程)对4个组进行取舍。您可以使用另一种队列机制。我通常使用LinkedBlockingQueue来等待其他线程将项目放置在队列中,这可能不是您想要的-这就是为什么我要轮询而不是调用take()。Take是等待的呼叫

private Future group1Future = null;
private Future group2Future = null;
private Future group3Future = null;
private Future group4Future = null;
private LinkedBlockingQueue<Callable> group1Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group2Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group3Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group4Queue
        = new LinkedBlockingQueue<>();

private ExecutorService executor = Executors.newFixedThreadPool(2);


public void startProcessing() {
    while (true) {
        if (group1Future != null && group1Future.isDone()) {
            if (group1Queue.peek() != null) {
                group1Future = executor.submit(group1Queue.poll());
            }
        }
        if (group2Future != null && group1Future.isDone()) {
            if (group2Queue.peek() != null) {
                group2Future = executor.submit(group2Queue.poll());
            }
        }
        if (group3Future != null && group3Future.isDone()) {
            if (group3Queue.peek() != null) {
                group3Future = executor.submit(group3Queue.poll());
            }
        }

        if (group4Future != null && group4Future.isDone()) {
            if (group4Queue.peek() != null) {
                group4Future = executor.submit(group4Queue.poll());
            }
        }
    }
}
private Future group1Future = null;
private Future group2Future = null;
private Future group3Future = null;
private Future group4Future = null;
private LinkedBlockingQueue<Callable> group1Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group2Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group3Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group4Queue
        = new LinkedBlockingQueue<>();

private ExecutorService executor = Executors.newFixedThreadPool(2);


public void startProcessing() {
    while (true) {
        if (group1Future != null && group1Future.isDone()) {
            if (group1Queue.peek() != null) {
                group1Future = executor.submit(group1Queue.poll());
            }
        }
        if (group2Future != null && group1Future.isDone()) {
            if (group2Queue.peek() != null) {
                group2Future = executor.submit(group2Queue.poll());
            }
        }
        if (group3Future != null && group3Future.isDone()) {
            if (group3Queue.peek() != null) {
                group3Future = executor.submit(group3Queue.poll());
            }
        }

        if (group4Future != null && group4Future.isDone()) {
            if (group4Queue.peek() != null) {
                group4Future = executor.submit(group4Queue.poll());
            }
        }
    }
}
private Future group1Future=null;
私有未来组2未来=空;
私有未来组3future=null;
private Future group4Future=null;
专用LinkedBlockingQueue组1队列
=新的LinkedBlockingQueue();
专用LinkedBlockingQueue组2队列
=新的LinkedBlockingQueue();
专用LinkedBlockingQueue组3队列
=新的LinkedBlockingQueue();
专用LinkedBlockingQueue组4队列
=新的LinkedBlockingQueue();
私有ExecutorService executor=Executors.newFixedThreadPool(2);
公共无效启动处理(){
while(true){
if(group1Future!=null&&group1Future.isDone()){
if(group1Queue.peek()!=null){
group1Future=executor.submit(group1Queue.poll());
}
}
if(group2Future!=null&&group1Future.isDone()){
if(group2Queue.peek()!=null){
group2Future=executor.submit(group2Queue.poll());
}
}
if(group3Future!=null&&group3Future.isDone(){
if(group3Queue.peek()!=null){
group3Future=executor.submit(group3Queue.poll());
}
}
if(group4Future!=null&&group4Future.isDone()){
if(group4Queue.peek()!=null){
group4Future=executor.submit(group4Queue.poll());
}
}
}
}

如果该组的任务未完成,它将跳到下一组。一次处理的组不会超过两个,并且任何单个组都不会运行多个任务。队列将强制执行有序执行

一个单线程执行器就可以了

ExecutorService  executorService = Executors.newSingleThreadExecutor();
它在内部使用带有
LinkedBlockingQueue的ThreadPoolExecutor

new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()))
new ThreadPoolExecutor(1,1,0L,TimeUnit.ms,
新建LinkedBlockingQueue()))

因此,您可以将其用于连续的工作,并可能对并发任务使用
多线程
执行器服务

如果您为每个组维护一个队列,您可以从每个队列中提取项目并将其送入线程池。下面的代码不会对任何一个组进行优先级排序,它只是以循环方式将它们拉入。如果您需要添加优先级,您应该能够轻松地添加。下面的代码将使用两个线程(加上管理队列的线程)循环4个组。您可以使用另一种队列机制。我通常使用LinkedBlockingQueue来等待其他线程将项目放置在队列中,这可能不是您想要的-因此我正在轮询而不是调用take()。Take是等待的呼叫

private Future group1Future = null;
private Future group2Future = null;
private Future group3Future = null;
private Future group4Future = null;
private LinkedBlockingQueue<Callable> group1Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group2Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group3Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group4Queue
        = new LinkedBlockingQueue<>();

private ExecutorService executor = Executors.newFixedThreadPool(2);


public void startProcessing() {
    while (true) {
        if (group1Future != null && group1Future.isDone()) {
            if (group1Queue.peek() != null) {
                group1Future = executor.submit(group1Queue.poll());
            }
        }
        if (group2Future != null && group1Future.isDone()) {
            if (group2Queue.peek() != null) {
                group2Future = executor.submit(group2Queue.poll());
            }
        }
        if (group3Future != null && group3Future.isDone()) {
            if (group3Queue.peek() != null) {
                group3Future = executor.submit(group3Queue.poll());
            }
        }

        if (group4Future != null && group4Future.isDone()) {
            if (group4Queue.peek() != null) {
                group4Future = executor.submit(group4Queue.poll());
            }
        }
    }
}
private Future group1Future = null;
private Future group2Future = null;
private Future group3Future = null;
private Future group4Future = null;
private LinkedBlockingQueue<Callable> group1Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group2Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group3Queue
        = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<Callable> group4Queue
        = new LinkedBlockingQueue<>();

private ExecutorService executor = Executors.newFixedThreadPool(2);


public void startProcessing() {
    while (true) {
        if (group1Future != null && group1Future.isDone()) {
            if (group1Queue.peek() != null) {
                group1Future = executor.submit(group1Queue.poll());
            }
        }
        if (group2Future != null && group1Future.isDone()) {
            if (group2Queue.peek() != null) {
                group2Future = executor.submit(group2Queue.poll());
            }
        }
        if (group3Future != null && group3Future.isDone()) {
            if (group3Queue.peek() != null) {
                group3Future = executor.submit(group3Queue.poll());
            }
        }

        if (group4Future != null && group4Future.isDone()) {
            if (group4Queue.peek() != null) {
                group4Future = executor.submit(group4Queue.poll());
            }
        }
    }
}
private Future group1Future=null;
私有未来组2未来=空;
私有未来组3future=null;
private Future group4Future=null;
专用LinkedBlockingQueue组1队列
=新的LinkedBlockingQueue();
专用LinkedBlockingQueue组2队列
=新的LinkedBlockingQueue();
专用LinkedBlockingQueue组3队列
=新的LinkedBlockingQueue();
专用LinkedBlockingQueue组4队列
=新的LinkedBlockingQueue();
私人遗嘱执行人