4 Replies Latest reply on Dec 12, 2009 7:59 AM by Sargis Harutyunyan

    ThreadPoolDispatcher shutdown issue

    Sargis Harutyunyan Newbie

      Hi All in ThreadPoolDispatcher there is code:




         @Destroy
         public void destroy()
         {
            executor.shutdown();
            try
            {
               executor.awaitTermination(5, TimeUnit.SECONDS);
            }
            catch (InterruptedException ie)
            {
               
            }
         }




      Now the question is: if I have long running task how to stop it because simple shutdown() is:


      Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.


      I think code should be like:



          @Destroy
          public void destroy() {
              executor.shutdown();
              try {
                  boolean isDone = executor.awaitTermination(5, TimeUnit.SECONDS);
                  if(!isDone) {
                      executor.shutdownNow();   
                  }
              } catch (InterruptedException ie) {
                  // ignore
              }
          }





      and shutdownNow() javadoc:


      There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.


      It means in my thread I can test for interrupted status and respond correspondingly.


      BTW do have other idea how to stop asynchronous started task?