java - ThreadPoolExecutor 意外错误

标签 java exception threadpoolexecutor

将一个简单的测试程序放在一起,应该并行执行一些任务。每次我们提交6个任务,等待完成。然后,又提交了一组任务。

 import java.util.concurrent.*;

public class ThreadExecutorTest {

  public static void main(String... args) {
    ThreadPoolExecutor ex   = new ThreadPoolExecutor(   15, 20, 10,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(5));

    for (int i = 0; i< 200; i++) {
      submitTasks(ex);
    }
    System.out.println("Done");
  }

  private static void submitTasks(ThreadPoolExecutor ex) {
    Future f1 = ex.submit( new SampleTask());
    Future f2 = ex.submit( new SampleTask());
    Future f3 = ex.submit( new SampleTask());
    Future f4 = ex.submit( new SampleTask());
    Future f5 = ex.submit( new SampleTask());
    Future f6 = ex.submit( new SampleTask());

//    System.out.println("Max Pool Size " + ex.getMaximumPoolSize());
    System.out.println("Pool Size " + ex.getPoolSize());
//    System.out.println("Active count " + ex.getActiveCount());
//    System.out.println("Task Count " + ex.getTaskCount());
//    System.out.println("Queue length " + ex.getQueue().size());
//    System.out.println("Queue remainingCapacity " + ((ArrayBlockingQueue)ex.getQueue()).remainingCapacity());

    try {
      f1.get();
    } catch (ExecutionException eex) {
      System.out.println("ExecutionException reported later - " + eex.getMessage());
    }catch(Exception exp){
      System.out.println("Exception reported later - " + exp.getMessage());
    }
    try{
      f2.get();
    }catch(Exception exp){}
    try{
      f3.get();
    }catch(Exception exp){}
    try{
      f4.get();
    }catch(Exception exp){}
    try{
      f5.get();
    }catch(Exception exp){}
    try{
      f6.get();
    }catch(Exception exp){}

  }

  static class SampleTask implements Callable<Void> {
    @Override
    public Void call() throws Exception {
      try {
//        Thread.sleep(300);
      } catch (Exception e) {
        System.out.println("Exception reported");
      }
      return null;
    }
  }
}

但是,发生了我无法解释的异常。我假设 ThreadPoolExecutor 配置正确,可以随时处理 6 个任务。

Pool Size 6
Pool Size 12
Pool Size 15
Pool Size 16
Pool Size 17
Pool Size 18
Pool Size 19
Pool Size 20
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@2328c243 rejected from java.util.concurrent.ThreadPoolExecutor@bebdb06[Running, pool size = 20, active threads = 0, queued tasks = 0, completed tasks = 53]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)

最佳答案

ThreadPoolExecutor.execute 有一条评论描述了它在提交新任务时的行为方式:

    /*
     * Proceed in 3 steps:
     *
     * 1. If fewer than corePoolSize threads are running, try to
     * start a new thread with the given command as its first
     * task.  The call to addWorker atomically checks runState and
     * workerCount, and so prevents false alarms that would add
     * threads when it shouldn't, by returning false.
     *
     * 2. If a task can be successfully queued, then we still need
     * to double-check whether we should have added a thread
     * (because existing ones died since last checking) or that
     * the pool shut down since entry into this method. So we
     * recheck state and if necessary roll back the enqueuing if
     * stopped, or start a new thread if there are none.
     *
     * 3. If we cannot queue task, then we try to add a new
     * thread.  If it fails, we know we are shut down or saturated
     * and so reject the task.
     */

在您的情况下,当您一次提交 6 个任务的批处理时,当池的当前大小小于核心大小时,这些提交会立即分派(dispatch)到新的工作线程(参见从 0 到 6 的跳转,从 6 到 12)。

一旦超过核心池大小但仍小于最大大小,只要队列未满,任务就会被提交到队列中,然后异步拉出以在现有工作线程上运行。由于这些任务都是背靠背提交的,因此很有可能所有六个任务都已提交,然后才被从队列中拉出;因此,前五个将排队,其余一个将进入上述过程的第 3 步:创建一个新的工作线程并立即运行该任务。 (这解释了后来从 15 到 16、16 到 17 等等的跳跃。)

最终,这导致线程池拥有最大数量的工作线程,当达到上述过程的第 3 步时(如上一段),Executor 无法创建新的工作线程并拒绝任务.本质上,即使有可用的工作线程,您也没有给执行者任何时间从队列中拉出任务以在过度填充队列之前对其执行。

关于java - ThreadPoolExecutor 意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51410322/

相关文章:

java - android.R 的 Android 开发问题

javax.el.PropertyNotFoundException : Property 'id' not found on type model. 杂货店

multithreading - 线程错误异常过多

java - 如何从 Java 在 Android 设备上运行 adb screenrecord 并结束屏幕录制?

java - 为什么我的 ThreadPool 没有在 Java 中并行运行?

java - Java 注释中的类型层次结构

Java Swing GUI 在重新聚焦时卡住

java - 什么是 NullPointerException,我该如何解决?

java - 如何从java中捕获的异常返回到特定点

java - 具有无界队列的 ThreadPoolExecutor 不创建新线程