解决Java线程池任务执行完毕后线程回收问题

 
 
      对于常常使用第三方框架进行web开发的程序员来讲,Java线程池理所应当是很是智能的,线程的生命周期应该彻底由Java自己控制,咱们要作的就是添加任务和执行任务。可是,最近作文档批量上传同步时发现线程池中的全部任务执行完毕后,线程并无中止,而后作了一个测试,发现确实如此:
 
问题及现象:
 
     public static void method1() {
          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() {
                  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()));
                  }
              });
          }
      }
debug模式下,任务执行完后,显示的效果以下:
也就是说,任务已经执行完毕了,可是线程池中的线程并无被回收。可是我在 ThreadPoolExecutor的参数里面设置了超时时间的,好像没起做用,缘由以下:
工做线程回收须要知足三个条件:

1)  参数allowCoreThreadTimeOut为truehtml

2)  该线程在keepAliveTime时间内获取不到任务,即空闲这么长时间程序员

3)  当前线程池大小 > 核心线程池大小corePoolSize。
 
解决一:
 
      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。
 
 
解决二:
 
      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参数失效。