美文网首页
Java线程池任务执行完毕后回收线程

Java线程池任务执行完毕后回收线程

作者: 舒尔诚 | 来源:发表于2020-06-25 10:46 被阅读0次

线程池中的所有任务执行完毕后,线程并没有停止,导致JVM出现OOM问题。后来查找了下面链接的资料,解决问题。

问题及现象:
public static void main(String[] args) {
BlockingQueue queue = new LinkedBlockingQueue();
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 10, TimeUnit.SECONDS, queue);
for (int i = 0; i < 20; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println(this.hashCode() / 1000);
for (int j = 0; j < 10; j++) {
System.out.println(this.hashCode() + ":" + j);
Thread.sleep(this.hashCode() % 2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(String.format("thread %d finished", this.hashCode()));
}
});
}
}

image.png

任务已经执行完毕了,但是线程池中的线程并没有被回收,程序任然处于运行状态。但是在ThreadPoolExecutor的参数里面设置了超时时间的,好像没起作用,原因如下:
工作线程回收需要满足三个条件:

参数allowCoreThreadTimeOut为true
该线程在keepAliveTime时间内获取不到任务,即空闲这么长时间
当前线程池大小 > 核心线程池大小corePoolSize。

解决一:allowCoreThreadTimeOut设置为true
public static void method1() {
BlockingQueue queue = new LinkedBlockingQueue();
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 1, TimeUnit.SECONDS, queue);
executor.allowCoreThreadTimeOut(true);
for ( int i = 0; i < 20; i++) {
executor.execute( new Runnable() {
public void run() {
try {
System. out.println( this.hashCode()/1000);
for ( int j = 0; j < 10; j++) {
System. out.println( this.hashCode() + ":" + j);
Thread. sleep(this.hashCode()%2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System. out.println(String. format("thread %d finished", this.hashCode()));
}
});
}
}

需要注意的是,allowCoreThreadTimeOut 的设置需要在任务执行之前,一般在new一个线程池后设置;在allowCoreThreadTimeOut设置为true时,ThreadPoolExecutor的keepAliveTime参数必须大于0。

解决二:调用shutdown方法

     public static void method1() {
          BlockingQueue queue = new LinkedBlockingQueue();
          ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 1, TimeUnit.SECONDS, queue);
          for ( int i = 0; i < 20; i++) {
              executor.execute( new Runnable() {
                  public void run() {
                      try {
                          System. out.println( this.hashCode()/1000);
                            for ( int j = 0; j < 10; j++) {
                               System. out.println( this.hashCode() + ":" + j);
                               Thread. sleep(this.hashCode()%2);
                          } 
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                      System. out.println(String. format("thread %d finished", this.hashCode()));
                  }
              });
          }         
          executor.shutdown();
      }

在任务执行完后,调用shutdown方法,将线程池中的空闲线程回收。该方法会使得keepAliveTime参数失效。

相关文章

  • Java线程池任务执行完毕后回收线程

    线程池中的所有任务执行完毕后,线程并没有停止,导致JVM出现OOM问题。后来查找了下面链接的资料,解决问题。 问题...

  • java线程池

    线程VS线程池 普通线程使用 创建线程池 执行任务 执行完毕,释放线程对象 线程池 创建线程池 拿线程池线程去执行...

  • 2019-03-25——Java并发 Executor框架 Th

    ThreadPoolExecutor是Java线程池的核心类,以内部线程池的形式对外提供管理任务执行,线程调度,线...

  • Java面试题——多线程

    Java面试题——多线程 1,什么是线程池? 线程池是多线程的一种处理方式,处理过程中将任务提交给线程池,任务执行...

  • 线程池

    什么是线程池? 线程池本质上是一种对象池,用来对线程进行管理 在执行任务时需要将线程从线程池取出 在任务执行完成后...

  • iOS多线程(三)

    任务1 2 子线程执行完毕后在执行任务3

  • 线程池

    线程池种类 ThreadPoolExecutor 基础线程池 线程执行任务过程 当前执行线程数 < corePoo...

  • Java源码-线程池

    一、线程池实现原理 Java支持多线程,多线程可以提高任务的执行效率。但是Java里面的线程跟操作系统的线程是一一...

  • 线程池源码分析

    线程池源码分析 在线程池执行任务的时候,都是主线程(可能是tomcat线程),将任务放到线程池中,再执行线程池的e...

  • 整整 Java 线程池

    整整 Java 线程池 用官方文档来说,线程池解决了两个问题: 一是在执行大量的异步任务时,因为线程池减少了任务开...

网友评论

      本文标题:Java线程池任务执行完毕后回收线程

      本文链接:https://www.haomeiwen.com/subject/fwqsfktx.html