6 Replies Latest reply on May 2, 2014 9:43 AM by suikast42

    JSR 236 Concurrency API - Kill threads with redeployment

    suikast42

      Hi,

       

      lets assume we have a simple lonrunning thread like shown below.

       

      ...
      
      run(){
      while(true){
           doJob();
      }
      ....
      
      
      
      }

       

      How can I kill this thread after a redeployment?

       

      I try this. But after a redeployment the thread is still alive.

       

      @Resource
      ManagedThreadFactory mtf ;

       

      @Resource
      ManagedExecutorService extExecutorService;

       

       

      @Test
      public void test() throws InterruptedException {
      //   Thread thread1 = mtf.newThread();
      Thread managedThread = mtf.newThread(new MyThread("MTF"));
      extExecutorService.submit(managedThread) ;
      Thread.sleep(5000);

       

      }

       

       

        • 1. Re: JSR 239 Concurrency API - Kill threads with redeployment
          emmartins

          You need to terminate the resources your app creates before undeploy, WildFly does not do that.

           

          In your specific case you would need to interrupt the thread and, since you run the thread in a executor (why?), you would also have to cancel the Future object returned on submit, if the execution was not done yet.

          • 2. Re: JSR 239 Concurrency API - Kill threads with redeployment
            suikast42
            You need to terminate the resources your app creates before undeploy, WildFly does not do that

             

            After reading this article  (page 18 ) from oracle I thought that the created thred's lifeclye is bounded to my deployed app. And every created thread over the threadfactory shoould be terminate if the application context is destroyed.

             

            In your specific case you would need to interrupt the thread and, since you run the thread in a executor (why?), you would also have to cancel the Future object returned on submit, if the execution was not done yet.

            I'm a little bit confused about the API. There is a ManagedThreadFacotry. So if I create a thread over this factory I can manually start my thread and over ManagedExcecutors I can obtain a future object right?

            • 3. Re: JSR 239 Concurrency API - Kill threads with redeployment
              emmartins

              Obviously I dunno about Oracle's implementation, but in WildFly it's the app that has to terminate resources it creates.

               

              Wrt your code, the ManagedThreadFactory it's useful to create custom executors, or create and managed directly your own threads, which you just start, and if not end themselves, kill it through interrupt. The ManagedExecutorService is a thread pool thus you submit runnable tasks instead of threads which, in case of not ending themselves, should be terminated through the Future object.

              • 4. Re: JSR 236 Concurrency API - Kill threads with redeployment
                suikast42

                The jsr 236 says on page 26

                ManagedExecutorService  instances may be terminated or suspended by the application server when applications   or components   are stopped or the application server itself is shutting down


                The may be in this sentence is the evil word, isn't it? :-D

                • 5. Re: JSR 236 Concurrency API - Kill threads with redeployment
                  emmartins

                  That sentence accepts multiple scenarios, and with respect to WildFly,  instances are sharable among applications, so we opted for "the application server itself is shutting down".

                  1 of 1 people found this helpful
                  • 6. Re: JSR 236 Concurrency API - Kill threads with redeployment
                    suikast42

                    Thank you well for that clarifiaction Eduardo.