2
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调用 after shutdown。在这种情况下,我该如何等待执行人?

4

1 回答 1

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

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

于 2013-02-03T08:47:12.803 回答