Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中使用执行器的串行调度队列?_Java_Multithreading_Executorservice_Threadpoolexecutor - Fatal编程技术网

Java中使用执行器的串行调度队列?

Java中使用执行器的串行调度队列?,java,multithreading,executorservice,threadpoolexecutor,Java,Multithreading,Executorservice,Threadpoolexecutor,我需要在Java中管理大量串行调度队列。需要多个引擎模块来管理各自的运行循环(其中一些模块可能会很快完成,其他模块可能会阻塞很长一段时间)。提交给每个引擎的作业必须按顺序运行 理想情况下,每个引擎都有一个线程池,可以在0到1个线程之间扩展,这使得ExecutorService既可以是串行的,也不会在有数百个线程的情况下占用大量资源,但只有少数线程可以看到活动 但是,当我尝试使用以下任一选项时: newthreadpoolexecutor(0,1,30L,TimeUnit.SECONDS,news

我需要在Java中管理大量串行调度队列。需要多个引擎模块来管理各自的运行循环(其中一些模块可能会很快完成,其他模块可能会阻塞很长一段时间)。提交给每个引擎的作业必须按顺序运行

理想情况下,每个引擎都有一个线程池,可以在0到1个线程之间扩展,这使得ExecutorService既可以是串行的,也不会在有数百个线程的情况下占用大量资源,但只有少数线程可以看到活动

但是,当我尝试使用以下任一选项时:

newthreadpoolexecutor(0,1,30L,TimeUnit.SECONDS,newsynchronousqueue());

newthreadpoolexecutor(1,1,30L,TimeUnit.SECONDS,newsynchronousqueue());
我发现当提交第二个作业时(如果第一个作业仍在运行),会抛出一个
RejectedExecutionException
,我假设是因为我有一个线程运行两个作业,而执行者不喜欢这样


我可以用我自己的队列实现这一点,并根据需要启动/停止/处置我自己的
线程
实例,但这似乎是
0您的特定问题来自于使用,正如文档中提到的:

同步队列没有任何内部容量,甚至没有1的容量

所以如果你用a来代替它,它实际上是有效的

然而,对于执行器,您可以使用的是一个线程,因为它在顶部使用一个线程来执行任务

一个简单的例子:

public static void main( String[] args )
{
    ExecutorService executor = Executors.newFixedThreadPool(1);
    TestThread t1 = new TestThread(1);
    TestThread t2 = new TestThread(2);
    executor.submit(t1);
    Future<?> f2 = executor.submit(t2);
    try {
        f2.get();
    } catch (InterruptedException | ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ThreadPoolExecutor tt = (ThreadPoolExecutor) executor;
    System.out.println(tt.getActiveCount()); //ensuring that there is no active threads in the pool after last thread terminates
}

public static class TestThread implements Runnable{
    private int id;

    public TestThread(int num){
        id = num;
    }
    public void run() {
        System.out.println("running thread: " + id);
        try {
            Thread.sleep(2000);
            System.out.println("After sleeping thread " + id);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
在最后一个线程终止后,没有活动线程

new ThreadPoolExecutor(1, 1, 30L, TimeUnit.SECONDS, new SynchronousQueue<>());
public static void main( String[] args )
{
    ExecutorService executor = Executors.newFixedThreadPool(1);
    TestThread t1 = new TestThread(1);
    TestThread t2 = new TestThread(2);
    executor.submit(t1);
    Future<?> f2 = executor.submit(t2);
    try {
        f2.get();
    } catch (InterruptedException | ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ThreadPoolExecutor tt = (ThreadPoolExecutor) executor;
    System.out.println(tt.getActiveCount()); //ensuring that there is no active threads in the pool after last thread terminates
}

public static class TestThread implements Runnable{
    private int id;

    public TestThread(int num){
        id = num;
    }
    public void run() {
        System.out.println("running thread: " + id);
        try {
            Thread.sleep(2000);
            System.out.println("After sleeping thread " + id);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
running thread: 1
After sleeping thread 1
running thread: 2
After sleeping thread 2
0