小编RJO*_*RJO的帖子

SingleThreadExecutor中java.util.concurrent.RejectedExecutionException的可能原因是什么

我在单例中创建以下执行程序:

   final private ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
        final ThreadFactory delegate = Executors.defaultThreadFactory();
        public Thread newThread(Runnable paramAnonymousRunnable) { 
            Thread localThread =      this.delegate.newThread(paramAnonymousRunnable);
            localThread.setName("MyTask-" + localThread.getName());
            localThread.setDaemon(XXX.this.daemonThread);
            return localThread;
        }
    });
Run Code Online (Sandbox Code Playgroud)

在程序执行期间,对单例的这种方法有很多调用.调用是在不同的线程中完成的,也许是在同一时间完成的.

private void send(final String paramString) {
  try {
      this.executor.execute(new Runnable() {
          public void run() {
              //DO some interesting stuff
          }
      });
  } catch (Exception localException) {
    this.handler.handle(localException);
  }
Run Code Online (Sandbox Code Playgroud)

}

在某些时候,以下堆栈开始出现:

java.util.concurrent.RejectedExecutionException
        at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774)
        at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656)
        at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:589)
        at XXXXX.send(XXXX.java:269)
Run Code Online (Sandbox Code Playgroud)

为什么jvm会抛出这样的异常?

singleThreadExecutor由LinkedBlockingQueue()支持.
线程池没有关闭.

有关信息,jvm是oracle jdk 1.6.单身是用弹簧创造的.从java.util.concurrent.Executors复制:

   public …
Run Code Online (Sandbox Code Playgroud)

java multithreading

51
推荐指数
3
解决办法
5万
查看次数

标签 统计

java ×1

multithreading ×1