ExecutorService.shutdown。当任务队列变空时,如何关闭执行程序?

时间:2013-02-03 08:37:20

标签: java threadpool

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class Main
{
    public static void main(String[] args)
    {
        final ExecutorService executor = Executors.newFixedThreadPool(10);
        executor.execute(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 10; i++)
                    executor.execute(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                             System.out.println("run");
                        }
                    });
            }
        });
        executor.shutdown();
        try
        {
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}

我得到RejectedExecutionException因为在executor.execute之后调用内部shutdown。在这种情况下,我如何等待执行人?

1 个答案:

答案 0 :(得分:2)

你必须知道何时不再有任务。这不是它可以猜到的东西。将关闭移动到第一个任务,它将按预期运行。即只有当您知道不打算添加更多任务时才会调用关机。

另一种方法是使用

final ExecutorService executor = new ThreadPoolExecutor(0, 10, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

这不会关闭执行程序,但它将停止所有线程,并且当您不再需要它时可以丢弃执行程序。注意:如果您以后添加更多任务,它将根据需要启动线程。

final ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 100, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
for (int i = 0; i < 1000; i++)
    executor.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            Thread.sleep(2);
            return null;
        }
    });
while (executor.getQueue().size() > 0) {
    System.out.println("Queue " + executor.getQueue().size() + ", Pool size " + executor.getPoolSize());
    Thread.sleep(200);
}
executor.setCorePoolSize(0);
while (executor.getPoolSize() > 0) {
    System.out.println("Pool size " + executor.getPoolSize());
    Thread.sleep(200);
}
System.out.println("Pool size " + executor.getPoolSize());

打印

Queue 946, Pool size 10
Queue 32, Pool size 10
Pool size 10
Pool size 0

然后退出。注意:如果任何线程仍在运行,程序将不会退出。