【问题标题】:Java ThreadPoolExecutor Hangs while processingJava ThreadPoolExecutor 在处理时挂起
【发布时间】:2015-08-25 06:44:42
【问题描述】:

我有一个自定义线程池执行器

public class CustomTPExecutor extends ThreadPoolExecutor
{

     /* Constructor called from my processor */

     public CustomTPExecutor (int corePoolSize, int maxPoolSize, long keepAliveTime,
  TimeUnit timeUnit, BlockingQueue<Runnable> blockingQueue,
  ThreadFactory threadFactory,Processor processor)
      {
        super(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, blockingQueue,threadFactory);
        this.processor = processor;
     }
     @Override
     protected void afterExecute(Runnable paramRunnable, Throwable paramThrowable) 
    {
      super.afterExecute(paramRunnable, paramThrowable);
      /* Notify the processor to get more records */
    }
}

在我的处理器中,我将为这个 customTPExecutor 创建实例并向它提交超过 1000 个任务。最大池大小为 5。

为了将它提交给执行者,我们使用它。

while(iterating map events containing 1000+records) {
 CustomThread tt = new TxnTCustomThread(eventMap.get(key), key,maxDate,connPool,connPool.getConnection(),email);
  executor.submit(tt); 
}

处理完所有记录后,我们调用执行器关闭方法。

我们注意到的一件事是,当线程正在执行时,它会在处理过程中挂起一段时间。它暂停一分钟左右,然后继续处理记录。我不确定这是什么原因。

我假设这是因为垃圾收集没有正确发生。我也无法从日志中获取任何信息来对此进行调试。在此过程中,java 的内存消耗大大减少。

任何有关如何解决此问题的建议都会非常有帮助。

【问题讨论】:

  • 建议加-verbose:gc查看stop the world pauses和GC基本信息。

标签: java multithreading garbage-collection executorservice threadpoolexecutor


【解决方案1】:

有“stop-the-world”GC 这样的东西,粗略地说,这意味着 JVM 决定它需要这样的“深度清理”,它会暂时停止所有线程以防止引用发生变化。这是不希望的,因此 GC 通常会尝试避免它,而倾向于在后台执行 GC。但这肯定会发生,尤其是当您使用所有核心进行计算(这可能会阻止后台 GC)并且您正在生成大量对象时。

如果您在线搜索“stop-the-world GC”,您会得到一些不错的结果。

【讨论】:

    猜你喜欢
    • 2013-12-08
    • 2022-10-06
    • 2020-07-22
    • 1970-01-01
    • 2013-02-25
    • 2011-02-03
    • 1970-01-01
    • 2016-09-11
    • 2020-06-05
    相关资源
    最近更新 更多