title: Grouping threads for interruption
body: <p>I'm facing some problem with thread grouping and it's an interruption (canceling some continuous process). I have one point of execution entry managed by <code>ThreadPoolTaskExecutor</code> and that works with <code>@Async</code> annotation on method. After an asynchronous invocation and run of this method, inside it I have some another async work to be completed by some <code>new Thread()</code>. Consider that I could have a situation when I have some those tasks running in parallel I need to identify and what task is it and <code>interrupt()</code> given <strong>task</strong> and it <strong>child threads</strong>  that created during task execution.</p>
  
  <p>I've investigated already some things about this problem. I could set some <code>ThreadGroup</code> for <code>ThreadPoolTaskExecutor</code>, get this group and <code>interrupt()</code> it. This could stop running task and threads created during task execution, but I need something like <strong>dynamic</strong> <code>ThreadGroup</code> depending on the task id, for not to stopping other running tasks, because setting <code>ThreadGroup</code> through <code>ThreadPoolTaskExecutor().setThreadGroup()</code> sets all Executor's tasks given group.
  Accessing <code>Future&lt;&gt;</code> of the created task and invoking <code>future.cancel()</code> helps to stop current task, but not <strong>child</strong> threads</p>
  
  <pre class="lang-java prettyprint-override"><code>    @Bean
      public TaskExecutor taskExecutor(){
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          executor.setCorePoolSize(5);
          executor.setMaxPoolSize(10);
          executor.setQueueCapacity(50);
          executor.setThreadNamePrefix("thread-");
          executor.initialize();
          return executor;
      }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>    public class Task {
         private Long id;
         //getters and setters
      }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>   @Service
      public class AsyncService {
  
          @Async("taskExecutor")
          public Future&lt;Task&gt; startAsyncProcess(Task task){
              //this thread is not killed by future.cancel()
              new Thread(() -&gt; System.out.println("Some continuous async db write")).start();
              //Some other continuous process, killed by future.cancel()
              return AsyncResult.forValue(task);
          }
  
      }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>    @Service
      public class TaskService {
  
          @Autowired
          private AsyncService asyncService;
  
          public void startProcess(Task task){
            Future&lt;Task&gt; future = asyncService.startAsyncProcess(task);
            // future.cancel();
          }
  
      }
  </code></pre>
  
  <p>I expect to make some complete interrupt of those threads. Maybe I need to create some manager for this and use <code>ThreadFactory</code> to generate those threads and separate them into small groups using <code>ThreadGroup</code>? Thanks in advance!</p>