解決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為true

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參數失效。
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM